while loop matrix problem

2 次查看(过去 30 天)
hi,
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.
i=1;
while i<imax
if z5(i,:) < z5(i-1,:)
c5(i,:) = k5(i,:);
elseif z5(i,:) > z5(i-1,:)
c5(i,:) = z5(i,:);
else
c5(i,:) = c5(i-1,:);
end
i=i+1;
end
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
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
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;
end
is more simply written as a for loop:
for i = 2:constant
%some code that doesn't change i or the constant
end
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
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
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 中查找有关 Industrial Statistics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by