Why doesn't the loop in my for loop work?

7 views (last 30 days)
Amanda S
Amanda S on 30 Jun 2020
Commented: Amanda S on 30 Jun 2020
Hi!
Im trying to get a for-loop to work with an if-statement, but I can't seem to get it to loop at all. Please help!
The snippet of the code that doesn't loop is bellow and Im sorry that the names are in swedish but it shouldn't be a problem...
The "besparingslistan" is a list with 3 coulmns and I want the for loop "f" to loop through all rows in it but only the first column, the same with "a" but only the second column. I don't know if I'm doing the for loops wrong in the beginning or if it just isn't working with "f=f+1" and "a=a+1" at the end.
If the "if-statement" is false, I want the loop to continue to the next row in the "besparingslistan" and therefore don't save anything in the "turkonfig".
for f = 1:Besparingslistan(:,1)
for a = 1:Besparingslistan(:,2)
if Kund_i_tur(f)~= Kund_i_tur(a)
Turkonfig = {f a};
else
f=f+1;
a=a+1;
end
end
end

Answers (2)

Sai Sri Harsha Vallabhuni
Sai Sri Harsha Vallabhuni on 30 Jun 2020
Hey,
Below is modified code which achieves what you are tying to do
for f = Besparingslistan(:,1)
for a = Besparingslistan(:,2)
if Kund_i_tur(f)~= Kund_i_tur(a)
Turkonfig = {f a};
end
end
end
Hope this helps.
  1 Comment
Amanda S
Amanda S on 30 Jun 2020
Hi! Thank you but it still doesnt work, or at least not the way that i thought it would. Perhaps there's a mistake where Turkonfig saves the values in a cell array? Isn't it just overwriting the old loop value when it just says Turkonfig = {f a}? Is there some way to save the f and a in seperate rows after each loop?

Sign in to comment.


Steven Lord
Steven Lord on 30 Jun 2020
Edited: Steven Lord on 30 Jun 2020
for f = 1:Besparingslistan(:,1)
This doesn't do what you think it does. Consider:
A = [20 1 2; 2 3 4; 3 4 5; 4 5 6];
for f = 1:A(:, 1)
disp("A(" + f + ", 1) is " + A(f, 1))
end
You will receive an error when f is 5, since A only has 4 rows. Instead of using the values in A to set the limits of your for loop, use the size of A.
A = [20 1 2; 2 3 4; 3 4 5; 4 5 6];
numberOfRows = size(A, 1);
for f = 1:numberOfRows
disp("A(" + f + ", 1) is " + A(f, 1))
end
Also, making changes to the loop variable inside the loop is discouraged. It doesn't do what you want anyway; any changes you made to that loop variable are thrown away when the next iteration of the loop starts and MATLAB takes the next value in the vector.
for k = 1:10
disp("k before changing is " + k)
k = 42;
disp("k after changing is " + k)
end
  1 Comment
Amanda S
Amanda S on 30 Jun 2020
Hmm alright, yeah I suspected that I was doing something wrong like that. But I still don't quite understand how I would do it instead? Lets say that I use the size of "Besparingslistan" instead, but then I get an error saying "Index exceeds the number of array elements (13)." What does that mean?
The "Besparingslistan" is a list with the size 169x3 built up with different combinations of 13 different customers in the first two columns and a savingsvalue in the third column. While Kund_i_tur and Turkonfig are column vectors with the customers, aka (1:1:13). Could it be something like the example with "You will receive an error when f is 5, since A only has 4 rows." ?
f = size(Besparingslistan,1);
a = size(Besparingslistan,2);
for forstatur = 1:f
for andratur = 1:a
if Kund_i_tur(f)~= Kund_i_tur(a)
Turkonfig = {f a};
end
end
end

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!