how to define dynamic nested loop python function - function

a = [1]
b = [2,3]
c = [4,5,6]
d = [a,b,c]
for x0 in d[0]:
for x1 in d[1]:
for x2 in d[2]:
print(x0,x1,x2)
Result:
1 2 4
1 2 5
1 2 6
1 3 4
1 3 5
1 3 6
Perfect, now my question is how to define this to function, considering ofcourse there could be more lists with values. The idea is to get function, which would dynamicaly produce same result.
Is there a way to explain to python: "do 8 nested loops for example"?

You can use itertools to calculate the products for you and can use the * operator to convert your list into arguments for the itertools.product() function.
import itertools
a = [1]
b = [2,3]
c = [4,5,6]
args = [a,b,c]
for combination in itertools.product(*args):
print combination
Output is
(1, 2, 4)
(1, 2, 5)
(1, 2, 6)
(1, 3, 4)
(1, 3, 5)
(1, 3, 6)

Related

How can I execute this function for range of values?

I have this function
where f has values (18 19 20 21 22), and I should compute the value of the function for each value of f and plot each value.
I try to make f as vector 18:22, but it gives result from 1 to 22. Same result when I use for loop. My code is below, how can I modify it to take the values within the range only?
clc
fc=20;
theta=80;
N=16;
f=18:22;
g_m(f)=(sin((N*pi/2).*sin(theta).*(f/fc-1)))./sqrt(N).*(sin(pi/2).*(f/fc-1));
g_p(f)=exp(1j*0.5*(N-1).*pi*sin(theta).*(f/fc-1));
gain(f)=g_m(f).*g_p(f);
figure(1);
plot(f,g_m(f));
I believe this should give you enough information:
f = 5:7;
g(f)= [2, 2, 5]
g =
0 0 0 0 2 2 5

Applying 'vector' of functions on a Matlab matrix

Let's say I've got a matrix with n columns, and I've got n different functions.
Is it possible to apply i-th function per each element in i-th column efficiently, that is without using loop?
For example for the following variables:
funs = #(x) [x, cos(x), x.^2]
A = [1 0 1
2 0 2
3 0 3
4 0 4] ;
I would like to obtain the following result:
B = [1 1 1
2 1 4
3 1 9
4 1 16] ;
without looping through columns...

Alternative to extract function when working with raster objects

I wonder how to sum pixel values of a raster (val_r) for each categories of another raster (cat_r). In other words, does an alternative to the function "extract" exist when working with raster objects? Thank you very much!
# sample raster with categories
cat_r<-raster(ncol=3,nrow=3, xmn=-10, xmx=10, ymn=-10, ymx=10)
cat_r[]<-c(1,2,1,3,4,3,4,4,4 ) #4 categories: 1, 2, 3 and 4
#sample raster with pixel values
val_r <-raster(ncol=3,nrow=3, xmn=-10, xmx=10, ymn=-10, ymx=10)
val_r[]<-c(1,0,1,5,2,5,2,2,2)
#extract function doesn't work for
extract(val_r, cat_r, fun=sum)
#I should find the following values: category 1: 2, cat 2: 0, cat 3: 10, cat 4: 8
You can use the zonal method:
library(raster)
cat_r <- raster(ncol=3,nrow=3, xmn=-10, xmx=10, ymn=-10, ymx=10, vals=c(1,2,1,3,4,3,4,4,4 ))
val_r <- setValues(cat_r, c(1,0,1,5,2,5,2,2,2))
zonal(val_r, cat_r, "sum")
# zone sum
#[1,] 1 2
#[2,] 2 0
#[3,] 3 10
#[4,] 4 8
This is equivalent to
s <- stack(cat_r, val_r)
v <- values(s)
tapply(v[,2], v[,1], sum)
# 1 2 3 4
# 2 0 10 8

Sort values that contain letters and symbols in a custom order

Can you change the MySQL sort by function? I am trying to sort my values according to an arbitrary order.
Currently looking for ways to inject a function that might help me out here short of adding a column and modifying the import.
This is the order I want:
AAA
AA+
AA
AA-
A+
A
A-
BBB+
BBB
BBB-
BB+
BB
BB-
B+
B
B-
CCC+
CCC
CCC-
CC
This is my result using sort by:
A
A+
A-
AA
AA+
AA-
AAA
B
B+
B-
BB
BB+
BB-
BBB
BBB+
BBB-
C
CC
CCC
CCC+
CCC-
EDIT:
Attempting but getting syntax errors:
CREATE FUNCTION sortRating (s CHAR(20))
RETURNS INT(2)
DECLARE var INT
CASE s
WHEN 'AAA' THEN SET var = 1
WHEN 'AA+' THEN SET var = 2
ELSE
SET VAR = 3
END CASE
RETURN var
END;
This is possible using the following syntax:
ORDER BY FIELD(<field_name>, comma-separated-custom-order)
for instance, if the expression you want to order by is called rating, then your ORDER BY clause would read:
ORDER BY FIELD(rating, 'AAA', 'AA+', 'AA', 'AA-', 'A+', 'A', 'A-',
'BBB+', 'BBB', 'BBB-', 'BB+', 'BB', 'BB-',
'B+', 'B', 'B-', 'CCC+', 'CCC', 'CCC-', 'CC')
Here's documentation on the FIELD FUNCTION
I see a pattern here:
BBB+
BBB
BBB-
BB+
BB
BB-
B+
B
B-
Think of each character as a column and sort each column in this order:
Letters
+
empty string
-
SELECT rating
FROM test
ORDER BY
MID(rating, 1, 1),
CASE MID(rating, 2, 1) WHEN '+' THEN 2 WHEN '' THEN 3 WHEN '-' THEN 4 ELSE 1 END,
CASE MID(rating, 3, 1) WHEN '+' THEN 2 WHEN '' THEN 3 WHEN '-' THEN 4 ELSE 1 END,
CASE MID(rating, 4, 1) WHEN '+' THEN 2 WHEN '' THEN 3 WHEN '-' THEN 4 ELSE 1 END
SQL Fiddle

How to multiply two rows or columns?

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.