If-statement, comparison of two vectors, both containing zero elements

1 次查看(过去 30 天)
Hi there, I want to compare two vectors that both contain elements equal to 0 in an if statement (so if vec1 < vec2) for example. This leads Matlab to skip the whole if loop, does anyone have an idea to solve this? Both vectors have the same length
vec1 = [1 0 3 6 43 56];
vec2 = [2 9 5 0 23 43];
if vec1 < vec2
disp('it works');
else
disp('it doesnt');
end
How can I solve this?

采纳的回答

madhan ravi
madhan ravi 2019-7-30
编辑:madhan ravi 2019-7-30
Wenn ich richtig verstehe, was du machen willst, dann:
vec1 = [1 0 3 6 43 56];
vec2 = [2 9 5 0 23 43];
for k = 1:numel(vec1)
if vec1(k) < vec2(k))
disp('it works');
else
disp('it doesnt');
end
end
  2 个评论
Johannes Schorr
Johannes Schorr 2019-7-30
编辑:madhan ravi 2019-7-30
Ich möchte jedes Element x der n-ten Reihe einer Matrix A mit jedem Element y der n-ten Reihe einer Matrix B vergleichen, genau. Die Dimensionen der Matrizen sind identisch.
Statt dem disp('text'); brauche ich jedoch eine logische Matrix, damit ich an genau den Stellen, wo das der Fall ist (A<B) eine Operation durchführen kann.
Habe aber gerade gemerkt, dass das ja viel einfacher ohne if-Statement geht:
log_vec = (vec1<vec2);
Vielen Dank trotzdem!
madhan ravi
madhan ravi 2019-7-30
Toll, ich wusste das, aber ich hab' wegen des Textes verwirrt. Viel Spaß beim Lernen.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by