Can I use a vector as index variable for a loop?

Hello, I have a question about the for loop. Why isn't it possible to use an element of a vector as the index variable?
Here is an example:
i_max=[3;2;4];
n=size(i_max,1);
i=zeros(n);
j=1;
for i(2)=0:1:i_max(1)
A(:,j)=i
j=j+1
end
Everytime I try this I get the error message "Unbalanced or unexpected parenthesis or bracket."
Is there any other way I can use the vector "i(x)" as index variable? I don't want to define a new variable like:
i_new=i(2)
This wouldn't really help me since my code must be very dynamic.
Thanks.

回答(2 个)

You have to think MATLAB.
Your code is messy and hard to read.
i_max=[3;2;4]; <== column vector of size 1x3
n=size(i_max,1); <== you can use length instead of size
i=zeros(n); <=== i is 3x3 matrix of zeros
j=1;
for i(2)=0:1:i_max(1) if you would like to update the i matrix, do it in the loop. This statement is illegal
A(:,j)=i <== you can't assign matrix to vector
j=j+1
end
I'd suggest to set your requirements of the code. if you would like to use array of indexes, you can do it like that:
i=(1:10)*2
idx=[1,3,6]
i(idx)
ans =
2 6 12
example for the for loop:
for n = 1:40
r(n) = rank(magic(n));
end

2 个评论

Well, thank you. I am a quite new to programming. That's why my code is messy.
I'll try your solution.
And btw i should be a 3x1 matrix. I messed something up there.
if you would like to have a zero matrix of the same size as i_max
i=zeros(size(i_max))

请先登录,再进行评论。

Well, you can't use loop variable that way. You can use nested for loop to assign new loop variable for the inner for loop. For example :
i_max=[3;2;4];
n=size(i_max,1);
i=zeros(n);
j=1;
for x = 1:length(i)
ii = i(x);
for ii = 0:1:i_max(1)
A(:,j)= ii;
j=j+1;
end
end
Note, however, that nested for loop is usually slow. You should try to vectorize your code. I can provide you with further example if you define more clearly what your programming problem is.

4 个评论

Thank you for you solution. I'll try that, too.
Here is the problem I want to solve. I've got the vector v with the length n. For example with n=3:
v=[3;4;2]
And I want to find all possible combinations of this vector like:
000
001
002
010
...
242
342
However the amount of solutions equals (v(1)+1)*(v(2)+1)*...*(v(n)+1). I've written this code for n=3 so far.
v=[3;2;4]
n=length(v)
i=zeros(n,1);
for i(n)=0:1:v(3)
for i(n-1)=0:1:v(2)
for i(n-2)=0:1:v(1)
A(:,j)=i;
j=j+1;
end
end
end
I want my code to generate solutions for all n>0. So I wrote the Code like for i(n)=.... to make it dependable from n. I know my code is messy since I am new to programming at all.
are you familiar with binary numbers? it is same idea:
0=000
1=001
2=010
3=011
4=100
5=101
6=110
7=111
I think you can do it with single for loop (or at most 2 for loops):
1 pass: go over all numbers. if there are more numbers, just copy the data x times where x is count of 0 to v(2)
2 pass: do the same, but each time write each number x times, where x is the v(2). if there are more numbers, just copy the data x times where x is count of 0 to v(3)
3 pass: do the same, but each time write each number x times, where x is the v(3). if there are more numbers, just copy the data x times where x is count of 0 to v(4)
...
I hope that this algo would be fine, but may have some wrong statements. please use it as guideline.
Try to think about your code before you write it.
Thank you for the suggestion.
I'll try that tomorrow.
I havn't thought that it was this easy.
v=[1;1;2]; %The vector I want to know all combinations off
n=length(v); %Number of Elements of "v"
k=prod(v+ones(n,1)); %Amount of all possible combinations
x=k; %Number of times the Matrix "a" is replicated
y=1; %Number of times the Kronecker tensor product is used
A=zeros(n, k); %Matrix of all solutions
for i=1:1:n
x=x/(v(i)+1); %Divides the times the Matrix a is replicated
B=ones(1, y); %Vector for the Kronecker
a=kron(0:1:v(i),B); %Kronecker tensor product - makes [0,1,2] and [1,1] to 0,0,1,1,2,2 f.ex.
A(i,:)=repmat(a,1,x); %Write a x-times to A
y=y*(v(i)+1); %Multiplies the times the Kronecker product is used
end
It gives me my solution really fast:
0 1 0 1 0 1 0 1 0 1 0 1
0 0 1 1 0 0 1 1 0 0 1 1
0 0 0 0 1 1 1 1 2 2 2 2
Thanks again for the hint with the binary numbers!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Surrogate Optimization 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by