Please answer! How to use an if function effectively?

1 回表示 (過去 30 日間)
Et.B200
Et.B200 2020 年 11 月 9 日
コメント済み: Stephen23 2020 年 11 月 10 日
Hi everyone,
I have a set data loaded into the workspace at 4x100, using the if function to analyse a certain colomn within that data to see say if any value is say greater than 2, and then say if a number within that coloum is greater than 2 then set a number on that same line from a different colomn say in the second coloum to a variable.
Eg, say coloum 4 is the colum i want to analyse, to see if any number in that colum is greater than 2, say lines 30, 67 and 80 are greater than 2, i want the function to then set column 2 values from lines 30, 67 and 80 to a variable lets say x. If that makes more sense
Thank you!

採用された回答

dpb
dpb 2020 年 11 月 9 日
編集済み: James Tursa 2020 年 11 月 9 日
colmn_analyze=4; % desired column to compare
colmn_target=2; % desired column to set
valLimit=2; % limiting value
% engine
ix=(A(:,colmn_analyze)>valLimit); % logical vector of locations match condition
A(ix,colmn_target)=x; % set the target column locations to value of x
NB: x must either be a scalar or a vector of length of TRUE elements in ix in the order to match or will have a number elements mismatch on the assignment.
Read up on logical indexing in the Getting Started doc for a most useful feature in MATLAB.
  7 件のコメント
Et.B200
Et.B200 2020 年 11 月 9 日
Ohh sorry, i've just worded my question wrong!! Sorry guys! So I want the output, using your example, to be the numbers 1.2684, 3.8009, and 0.1378, not to change the data set as such, but to output the numbers on the same line as those in another column which suit the value limit.
Many thanks, Ethan
James Tursa
James Tursa 2020 年 11 月 9 日
Do you mean like this?
colmn_other = 6;
A(ix,colmn_other) = A(ix,colmn_target)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by