Loop in bigquery - loops

Can you loop through a table in bigquery? with a for/while statement
Example, I need.
products= [1,2, 4, 6, 8]
variable= 0 //initialize variable
for variables in variable
variable1 = variable
print(variables , variable1 )
products variable1
A 1
B 2
C 3
D 4
E 5

Related

Generate an array with specific duplicate elements in MATLAB

I have one array, for example B = [2,5,7], and also have a number C = 10, where C is always larger than or equal to the largest number in B.
and I want to generate an array A according to B and C. In this specific example, I have
A = [1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 7, 8, 9, 10]
that is, I generate an array [1:C], but with each element in B are duplicated 3 times. Is there any good way that does not use for loop to generate array A?
Thank you!
You can use repelem (introduced in Matlab R2015a):
B = [2 5 7]
C = 10;
n = 3;
r = ones(1,C);
r(B) = n;
A = repelem(1:C, r)
How about...
B = [2,5,7];
C = 10;
A = sort([1:C,B,B])
I think the answer of #RPM could be faster. But because you specifically asked for a solution without sort:
B = [2,5,7];
C = 10;
D = setdiff(1:C,B)-1;
A = reshape(repmat(1:C,3,1),1,3*C);
A([3*D+1,3*D+2]) = [];
which will also return the correct result. I'm not to sure about the order setdiff() has. It might be worse than sort() in all cases. Especially with
A = sort([1:C,B,B]) as the input is already almost in order.
Following the same philosophy as this solution to Repeat copies of array elements: Run-length decoding in MATLAB, you can do something similar here, like this -
%// Get increment array (increments used after each index being repeated in B)
inc = zeros(1,C);
inc(B+1) = N-1
%// Calculate ID array (places in output array where shifts occur)
id = zeros(1,C+(N-1)*numel(B));
id(cumsum(inc) + (1:C)) = 1
%// Finally get cumulative summation for final output
A = cumsum(id)
Sample run -
B =
2 5 7
C =
10
N =
3
inc =
0 0 2 0 0 2 0 2 0 0
id =
1 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1
A =
1 2 2 2 3 4 5 5 5 6 7 7 7 8 9 10

Group array elements

I have the following array: A = [2 7 8 9 10] and I'm looking for a way to group its subsequent elements, so to get a result like B = [1 2; 4 7], where the first column returns the number of subsequent elements, and the second the value of the first element.
How do you suggest to approach the problem?
Try this:
idx = find([[0 diff(A)] ~= 1 1]);
B = [diff(idx); A(idx(1:end-1))].';
The logic is this:
You're interested to know when a subsequent sequence starts. You could use the diff function to calculate the difference between each element and the previous one, like this:
>> A = [2 7 8 9 10]
>> diff(A)
ans =
5 1 1 1
We want to focus on values different than 1 (because they are within a sequence). The 5, in this case, represents the start of the 7,8,9,10 sequence. Also, the first element always start a sequence. We may "force" this by adding a 0 to the response, like this:
>> [0 diff(A)]
ans =
0 5 1 1 1
Now, we need to get the numbers different than 1:
>> [0 diff(A)] ~= 1
ans =
1 1 0 0 0
As we want to know the length of the sequence, it would be interesting to know the end of the last sequence. For that, we add a 1 in the end:
>> [[0 diff(A)] ~= 1 1]
ans =
1 1 0 0 0 1
Now we use find to get the indexes of the 1's:
>> idx = find([[0 diff(A)] ~= 1 1])
ans =
1 2 6
It tells us we have two sequences: the first one starts on 1 and ranges from 1..2-1, and the second one starts on 2 and ranges from 2..6-1. If we do a diff of idx, we get the lenghts:
>> diff(idx)
ans =
1 4
To get the values, we index A using idx (ignoring the last value):
>> A(idx(1:end-1))
ans =
2 7
The last line just combines this into a row matrix, and transposes it:
>> B = [diff(idx); A(idx(1:end-1))].'
ans =
1 2
4 7

How to insert elements according to array of indices in MATLAB?

Say, I have Index array I = [2 4 6]
Another, array A =[1 0 0]
I want to insert elements of array A in array C at position 2 , 4 and 6.
Array C is initially empty.
Run 2: I = [1, 7, 8]
A = [0 0 1]
I would want to insert elements of array A in array C at position 1 , 7 and 8.
And, so on.
Please help.
Thanks.
Cheery essentially answered the question for you, but in order to be complete, simply use the array I and index into C and use I to place the values of A into the corresponding slots in C. As such:
C(I) = A;
If C was not already allocated, then C will pad whatever you didn't index with zeroes. As such, given your two examples, this is what we get:
I1 = [2 4 6];
I2 = [1 7 8];
A1 = [1 0 0];
A2 = [0 0 1];
C1(I1) = A1
C2(I2) = A2
C1 =
0 1 0 0 0 0
C2 =
0 0 0 0 0 0 0 1
However, because your array A already has zeroes, you can't really see the effect of this type of assignment. If you change up your array A into some other values that don't include zero, then you'll see that this does work.

How to evaluate multiple limits in a matrix array MATLAB

this is my question:
Suppose the next 2 arrays:
Z = [1 2;3 6;2 4];
Y = [1 2 3 5 2 3 5 7 1 0 4 6]
Now I want to get something like this:
X = {[1 2];[3 5 2 3];[2 3 5]}
As you can see, variable "X" has the corresponding values of vector "Y", which positions are contained by variable Z. I was thinking in something like this:
fun = #(c) Y(c(1,1):c(1,2));
X = arrayfun(fun,Z)
But it doesn't work :(, any idea?
X = arrayfun(#(n) Y(Z(n,1):Z(n,2)), 1:size(Z,1), 'uni', 0);
bsxfun approach -
t1 = bsxfun(#times,1:numel(Y),ones(size(Z,1),1))
t2 = bsxfun(#ge,t1,Z(:,1)) & bsxfun(#le,t1,Z(:,2))
t3 = bsxfun(#times,t2,Y)
X = cellfun(#nonzeros,mat2cell(t3,ones(1,size(Z,1)),numel(Y)),'uni',0)

Get contents of array from string containing name of array

Let's say I have the following code:
#!/bin/bash
arrayName_1=( 3 4 5 )
arrayName_2=( 0 1 2 )
str="arrayName_1"
arrayName=?
In the end, I want arrayName variable to be an array containing 3, 4, 5, just like arrayName_1. How do I make this happen? I know I could do the below, but I have to use str instead of arrayName_1:
arrayName=("${arrayName_1[#]}")
Try this:
arrayName_1=( 3 4 5 )
arrayName_2=( 0 1 2 )
name="arrayName_1"
indirect=$name"[#]"
arrayName=("${!indirect}")

Resources