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

66 views (last 30 days)
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.

Answers (2)

michael
michael on 3 Oct 2016
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 Comments
Rene Stiller
Rene Stiller on 3 Oct 2016
Edited: Rene Stiller on 3 Oct 2016
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.
michael
michael on 3 Oct 2016
if you would like to have a zero matrix of the same size as i_max
i=zeros(size(i_max))

Sign in to comment.


Joe Yeh
Joe Yeh on 3 Oct 2016
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 Comments
Rene Stiller
Rene Stiller on 3 Oct 2016
Thank you for the suggestion.
I'll try that tomorrow.
Rene Stiller
Rene Stiller on 4 Oct 2016
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!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!