MATLAB Answers

Insert zero in an array when a certain value is exceeded

5 ビュー (過去 30 日間)
luca
luca 2019 年 10 月 14 日
回答済み: dpb 2019 年 10 月 14 日
Hi given arrays
Y= [175 175 175 175 175 175 175 175 175 175 175 175];
SETT=[178 130 120 140 100 160 179 165 157 140 130 180 140];
and
T = [25 60 50 40 30 30 30 35 10 34 23 45 12];
Considering each column, I want to create a new vector U where I put the value of T where the element on the same column of SETT does not exceed the value in Y. And put zeros in the values exceeded
So in this case I want to obtain
U=[0 60 50 40 30 30 0 35 10 34 23 0 12].
May someone helo me with the code?

  0 件のコメント

Sign in to comment.

回答 (2 件)

Star Strider
Star Strider 2019 年 10 月 14 日
The vectors do not have the same numbers of elements.
Correcting for that:
Y= [175 175 175 175 175 175 175 175 175 175 175 175];
SETT=[178 130 120 140 100 160 179 165 157 140 130 180 140];
T = [25 60 50 40 30 30 30 35 10 34 23 45 12];
SETT = SETT(1:numel(Y)); % Equalise Vectors
T = T(1:numel(Y)); % Equalise Vectors
idx = SETT <= Y; % Logical Index Vector
U = T.*idx
producing:
U =
0 60 50 40 30 30 0 35 10 34 23 0

  0 件のコメント

Sign in to comment.


dpb
dpb 2019 年 10 月 14 日
U=T;
U(SETT>175)=0;

  0 件のコメント

Sign in to comment.

タグ

製品


リリース

R2019b

Translated by