load cities.mat;
temp_ratings = sort(ratings,'descend');
for j=1:9
i=0;
temp_vec=zeros(1,9);
while temp_vec==zeros(1,9)
i=i+1
if temp_ratings(i+1)~=temp_ratings(i+2)
temp_vec=temp_ratings(i+2);
end
end;
end;
This is a code that i'm trying to write for homework. I need to find the 2nd Highest number in each column. I made easier codes, even without loops for this one, but i'm trying to make it a lil bit more sophisticated. The thing is that in some of the columns sometimes there're more than 1 elements that are presenting the highest number, so I need to take that in account.
This is the only direction that I'm trying to do, so i'm not looking too much for a new command or something more than I'm using here. Anyway - The problem with my code that it does what I want but only for the first column! I mean that the while command is working and after it gets the 'if' and stops the loop (of 'for' command).
What am i missing here? Thanks, Eran.

 Accepted Answer

dpb
dpb on 12 Apr 2017

0 votes

What you're missing here is the definition of 'True' in Matlab for vectors/arrays in if and while and other logical tests. From the doc's for while there's the following--
"while...repeats the execution of a group of statements in a loop while the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
The problem is your while condition is true iff there's never anything but 0 in temp_vec; as soon as you insert anything nonzero there, then temp_vec==zeros(1,9) will be false as every element in the resulting logical vector has to be true for the expression to be true.
Since you're trying as homework, I'll leave the hint at that for now...

7 Comments

Eran Sandman
Eran Sandman on 12 Apr 2017
Edited: Eran Sandman on 12 Apr 2017
I'm using this code now:
vec = [10,10,10,7,5;9,9,5,4,1;5,4,3,2,1]';
temp_vec=zeros(1,3)
i=0;
for j=1:3
while temp_vec(1,j)==0
i=i+1
if vec(i,j)>vec(i+1,j)
temp_vec(1,j)=vec(i+1,j)
end
end
end
When I broke the code for 3 "while's" which instead of the 'j' i put 1 , 2 ,3 for every "while". It worked perfectly of course. So my logic says that this code correct, but I don't get it why it's not.
If the condition is met on J=1 ,then temp_vec(1,1) for example changes to something different from 0, which stops the while. why does it stops the loop to move to the 2nd column and do the same? :( more hints please! :)
p.s i used for every while also a different letter (i.e i,j,n)
and thanks btw!
dpb
dpb on 12 Apr 2017
Edited: dpb on 12 Apr 2017
Have you set a breakpoint and stepped through to see what happens on each iteration? Sometimes an aha! moment that way is more enlightening than being pointed at from afar might be. If that doesn't get you home, don't hesitate to come back... :)
HINT: The problem now is not the if, exactly, it has to do with an initialization issue...in fact, it should give you an execution error as written that should be that AHA! moment of which I spoke earlier... :)
Eran Sandman
Eran Sandman on 13 Apr 2017
Edited: Eran Sandman on 13 Apr 2017
i got it working! in the if condition i put i=0, now when i'm looking at the code running it i don't see it jumping between J(from 1 to 3), but it seems it does because after the if condition is met it starts a new column. is that the correct method ?
thanks
dpb
dpb on 13 Apr 2017
Ayup... :)
Thank you so much! that AHA! moment was exactly like that :D
dpb
dpb on 13 Apr 2017
Then how about Accept ing the answer to at least indicate the thread has reached a satisfactory resolution?
I did figure that would be the case when it became clear... :) The uncertainty about logical if and arrays in Matlab always takes some time to come to grips with; it makes sense given how Matlab works and is quite powerful when understand the uses for logical arrays, but isn't intuitive at first.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 12 Apr 2017

Commented:

dpb
on 13 Apr 2017

Community Treasure Hunt

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

Start Hunting!