while loop matrix problem

조회 수: 6 (최근 30일)
Galgool 2019년 7월 26일
댓글: Guillaume 2019년 7월 29일
I want to use a while loop on matrices, to define a new matrix by calculating one row each time.
I'm trying to do it without creating another loop that will go over the columns. Is that possible?
for example:
z5 and k5 are known matrices. This loop defines c5.
while i<imax
if z5(i,:) < z5(i-1,:)
c5(i,:) = k5(i,:);
elseif z5(i,:) > z5(i-1,:)
c5(i,:) = z5(i,:);
c5(i,:) = c5(i-1,:);
It's not doing what I want it to do and I'm not sure what exactly its doing.
Would appreciate any help and clarification on this matter.
  댓글 수: 10
Torsten 2019년 7월 26일
편집: Torsten 2019년 7월 26일
I'm talking about the "if z5(i,:) < z5(i-1,:) ", not about the "v = z5(i,:) < z5(i-1,:) "
Read my answer carefully.
the cyclist
the cyclist 2019년 7월 26일
편집: the cyclist 2019년 7월 26일
To reiterate what Torsten is saying ...
To enter an if statement on a vector condition, all elements of that vector have to evaluate to true. MATLAB doesn't discern the if condition element-by-element.
That is the fatal flaw in your algorithm

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume 2019년 7월 26일
@galgool, before calling someone wrong please learn the language properly, in particular how if works when given a vector as an expression. From the doc:
An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.
So, let's look at
if z5(i,:) < z5(i-1,:)
The expression is
z5(i,:) < z5(i-1,:)
The result of that expression is a logical vector (array of 0s and 1s). As per the rule above, the if is true only if all the elements of the vector are 1. I.e. the only time the expression is true is when z5(i, j) < z5(i-1, j) for all j. In general, you should avoid passing a vector to if because people don't know the rule above. The expression you've written is exactly equivalent to:
if all(z(5(i,;) < z5(i-1,:))
which is clearer.
As others have said, you will also have to loop over the columns.
Note that:
i = 2; %your code would error if you actually started at 1!
while i < constant
%some code that doesn't change i or the constant
i = i+1;
is more simply written as a for loop:
for i = 2:constant
%some code that doesn't change i or the constant
Note that if you didn't have the dependence on the previous c5 row, when z5(i,j) == z5(i-1, j), the whole thing could written without any loop at all.
  댓글 수: 4
Galgool 2019년 7월 29일
Its difficult for me to understand the lines you wrote, as you put the inequality condition within the columns of c5, k5 and z5. doesnt that just give zeros and ones for the columns index?
And another thing - is it possible to get it done without a loop? the only eason I used a loop is because c5(i) is dependent on c5(i-1), z5(i-1) and k5(i-1)...
Guillaume 2019년 7월 29일
Maybe, this is clearer this way:
mask = z5(i, :) < z5(i-1, :); c5(i, mask) = k5(i, mask); %set c5 values for which mask is true to matching k5 values
mask = z5(i, :) > z5(i-1, :); c5(i, mask) = z5(i, mask); %set c5 values for which mask is true to matching z5 values
mask = z5(i, :) == z5(i-1, :); c5(i, mask) = c5(i-1, mask); %set c5 values for which mask is true to matching value on previous row
If there was the dependence on the previous row of c5, this could be done trivially without a loop, but unfortunately with that dependence it does need the loop... unless you are guaranteed not to have two consecutive identical value in any z5 column.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)


Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!

Translated by