Related
I am new to Octave, so I was reading documentation and I found sub2ind function. I started to test it, but sometimes it works weird or I just don't understand how it must work.
So this is how subscripts must be converted to linear indices. (Example from documentation)
[(1,1), (1,2), (1,3)] [1, 4, 7]
[(2,1), (2,2), (2,3)] ==> [2, 5, 8]
[(3,1), (3,2), (3,3)] [3, 6, 9]
And this is another example from documentation
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
So if we look at the first example the (2, 2) == 5, but second example says [2, 2] == 2.
The (1, 3) has different results too.
Practically It works as the second example shows.
If I try to use this function with only 1 pair it return the same pair
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
In this test I can't see any relation between input and output
sub2ind([3, 3], [2, 2], [3, 3])
# ans = [8, 8]
Function works this strange(maybe not) way only when it gets 1 pair or when one of pairs is pair kind [x, x](two same values).
But otherwise it works fine, so this test returns that it should:
sub2ind([3, 3], [2, 1], [1, 3])
# ans = [2, 7]
Also it works fine when this variant is used sub2ind (dims, i, j).
How does the function works?
You misunderstand the input format.
Change
s1 = [2, 2];
s2 = [1, 3];
ind = sub2ind ([3, 3], s1, s2)
⇒ ind = 2 8
to this:
row = [2, 2]; % x1 and x2
col = [1, 3]; % y1 and y2
ind = sub2ind ([3, 3], row, col)
⇒ ind = 2 8
You have two inputs that you convert to linear indices:
[x1, y1] = [2, 1] = 2 and [x2 y2] = [2, 3] = 8.
This:
sub2ind([3, 3], [2, 2])
# ans = [2, 2]
appears to be equivalent to:
sub2ind([3, 3], [2, 2], [1, 1])
even though it's not in the documentation.
I can't seem to be able to create a function that takes two lists as arguments and returns how many elements there are common in both lists.
e.g. f [1, 2, 4, 2] [2, 3, 4, 4] returning 2 (repetitions are ignored).
Any suggestions? I tried this
*Main> a = [1, 2, 3]
*Main> b = [2, 3, 4]
*Main> [x | x <- a, x <- b]
[2,3,4,2,3,4,2,3,4]
Then I was planning to use the length function to know how many item there are in common.
You don't want to extract an x from both lists; extract from one list, and check if it is present in the other.
> a = [1,2,3]
> b = [4,3,2]
> [x | x <- a, x `elem` b]
[2,3]
> [x | x <- b, x `elem` a]
[3,2]
Note that the order in which items appear in the result depends on the order in which they appear in the list you pull from.
I have this function:
def applychange(L):
result=3+2
L=[4,5,6]
return result
L = [1,2,3]
print(applychange(L))
print(L)
and the result is :
5
[1, 2, 3]
how to change the L variable to new value to have this result:
5
[4, 5, 6]
If your goal is to modify L as global variable you need to declare it as such.
For your code to run as intended, insert global L after the result assignment in your function.
Additionally, there is no need to pass L to the function in order to modify L.
def applychange():
result = 3 + 2
global L
L = [4, 5, 6]
return result
L = [1, 2, 3]
print(applychange())
print(L)
Your code written this way will result in the intended output:
5
[4, 5, 6]
The problem is that you overwrite the variable L, instead of modifying it. You can use
L[:] = [4,5,6]
instead of
L = [4,5,6]
a = [1, 2, 3];
b = [3, 2, 1];
c = a * b;
yields
error: operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)
Why can I not multiply these two rows of the same size?
I shouldn't have to run a for loop for this, but I don't know of another way...
I saw section 1.2.3 here, which indicates (to me at least) that I should be able to do it.
You made 2 rows, which can't be multiplied together.
The general form of matrix multiplication is "Row-Dot-Column", which means take the dot product of each row with each column. In your case you have 1 row, but 3 columns (which doesn't work!).
a = [1, 2, 3];
b = [3, 2, 1];
c = a' * b;
ans =
3 2 1
6 4 2
9 6 3
I see now that there is a .* operator. I did not know where to find that in the documentation, and it does what I want.
I would like to submit a data frame to a function and use it to subset another data frame.
This is the basic data frame:
foo <- data.frame(var1= c(1, 1, 1, 2, 2, 3), var2=c('A', 'A', 'B', 'B', 'C', 'C'))
I use the following function to find out the frequencies of var2 for specified values of var1.
foobar <- function(x, y, z){
a <- subset(x, (x$var1 == y))
b <- subset(a, (a$var2 == z))
n=nrow(b)
return(n)
}
Examples:
foobar(foo, 1, "A") # returns 2
foobar(foo, 1, "B") # returns 1
foobar(foo, 3, "C") # returns 1
This works. But now I want to submit a data frame of values to foobar. Instead of the above examples, I would like to submit df to foobar and get the same results as above (2, 1, 1)
df <- data.frame(var1=c(1, 1, 3), var2=c("A", "B", "C"))
When I change foobar to accept two arguments like foobar(foo, df) and use y[, c(var1)] and y[, c(var2)] instead of the two parameters x and y it still doesn't work. Which way is there to do this?
edit1: last paragraph clarified
edit2: var1 type corrected
Try this:
library(plyr)
match_df <- function(x, match) {
vars <- names(match)
# Create unique id for each row
x_id <- id(match[vars])
match_id <- id(x[vars])
# Match identifiers and return subsetted data frame
x[match(x_id, match_id, nomatch = 0), ]
}
match_df(foo, df)
# var1 var2
# 1 1 A
# 3 1 B
# 5 2 C
Your function foobar is expecting three arguments, and you only supplied two arguments to it with foobar(foo, df). You can use apply to get what you want:
apply(df, 1, function(x) foobar(foo, x[1], x[2]))
And in use:
> apply(df, 1, function(x) foobar(foo, x[1], x[2]))
[1] 2 1 1
To respond to your edit:
I'm not entirely sure what y[, c(var1)] means, but here's an attempt at trying to figure out what you are trying to do.
What I think you were trying to do was: foobar(foo, y = df[, "var1"], z = df[, "var2"]).
First, note that the use of c() is not needed here and you can reference the columns you want by placing the name of the column in quotes OR reference the column by number (as I did above). Secondly, df[, "var1"] returns all of the rows for the column names var1 which has a length of three:
> length(df[, "var1"])
[1] 3
The function you defined is not set up to deal with vectors of length greater than 1. That is why we need to iterate through each row of your dataframe to grab a single value, process it, and then go to the next row in the data.frame. That is what the apply function does. It is equivalent to saying something along the lines of for (i in 1: length(nrow(df)) but is a more idiomatic way of handling such issues.
Finally, is there a reason you generated var1 as a factor? It probably makes more sense to treate these as numeric in my opinion. Compare:
> str(df)
'data.frame': 3 obs. of 2 variables:
$ var1: Factor w/ 2 levels "1","3": 1 1 2
$ var2: Factor w/ 3 levels "A","B","C": 1 2 3
Versus
> df2 <- data.frame(var1=c(1,1,3), var2=c("A", "B", "C"))
> str(df2)
'data.frame': 3 obs. of 2 variables:
$ var1: num 1 1 3
$ var2: Factor w/ 3 levels "A","B","C": 1 2 3
In summary - apply is the function you are after here. You may want to spend some time thinking about whether your data should be numeric or a factor, but apply is still what you want.
foobar2 <- function(x, df) {
.dofun <- function(y, z){
a <- subset(x, x$var1==y)
b <- subset(a, a$var2==z)
n <- nrow(b)
return (n)
}
ans <- mapply(.dofun, as.character(df$var1), as.character(df$var2))
names(ans) <- NULL
return(ans)
}