Please answer! How to use an if function effectively?

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

What is a in this text, is it the name that the data is stored under in my workspace?
Note: Edited the ; row index to be :
@Ethan: A in this context is simply the variable that holds your data. Use whatever your actual variable name is in this code instead of A.
Et.B200
Et.B200 2020년 11월 9일
편집: Et.B200 2020년 11월 9일
I still get an error message, ive inputted the a value as my workplace variable with the data? And thank you James for letting me know its a :, the error message reads that the x variable is undefined
Example run:
>> A = 4*rand(5,5) % arbitrary sample data
A =
2.8242 3.2938 1.7550 1.9591 1.1041
0.1273 2.7793 1.5262 1.7823 2.7188
1.1077 1.2684 3.0621 2.5853 2.6204
0.1847 3.8009 3.1808 2.8375 0.6504
0.3885 0.1378 0.7475 3.0187 0.4760
>> colmn_analyze=4; % desired column to compare
>> colmn_target=2; % desired column to set
>> valLimit=2; % limiting value
>> x = 10;
>> ix=(A(:,colmn_analyze)>valLimit); % logical vector of locations match condition
>> A(ix,colmn_target)=x
A =
2.8242 3.2938 1.7550 1.9591 1.1041
0.1273 2.7793 1.5262 1.7823 2.7188
1.1077 10.0000 3.0621 2.5853 2.6204
0.1847 10.0000 3.1808 2.8375 0.6504
0.3885 10.0000 0.7475 3.0187 0.4760
So in column 4 the 3rd, 4th, and 5th elements are over the valLimit of 2. So those elements in column 2 get changed to 10 (i.e., x).
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
Do you mean like this?
colmn_other = 6;
A(ix,colmn_other) = A(ix,colmn_target)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

질문:

2020년 11월 9일

댓글:

2020년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by