How to replace the elements of a matrix using the conditions if,else?

I want to replace the elements of a matrix
using different conditions. For instance, let all
elements larger than 0.5 be replaced by -1, else
keep the way it is.
So I thought I should simply write the command below:
X=rand(10,10);
if X(:,:)>0.5;
T(:,:)=-1;
else T=X(:,:);
end;
but it does not work because T==X.
Could someone tell me how to correct these command lines?
Thank you
Emerson

 채택된 답변

Matt Fig
Matt Fig 2011년 6월 5일
IF statements do not pick out elements of an array like you are imagining that they do. When you write this:
if conditional
% Do something
end
for non-scalar conditional, the IF statement will pass if and only if all of the elements in conditional are true or non-zero. For example:
x = [1 2 3]; % All are non-zero, passes conditional...
if x
disp('In if')
end
but now change it to:
x = [1 2 0]; % All are not non-zero, fails conditional...
if x
disp('In if')
end
So the way you have to do what you want is either through logical indexing, or by single value through iteration.
% iteration approach - look at one value at a time!
x = [.1 .2 .3 .7 .8 .9 .1 .2 .3]; % Work with this array.
T = zeros(size(x)); % Make another array to fill up...
for ii = 1:length(x)
if x(ii)>.5
T(ii) = 99;
else
T(ii) = -100;
end
end
% Logical indexing approach
x = [.1 .2 .3 .7 .8 .9 .1 .2 .3]; % Work with this array.
T(x>.5) = 99;
T(x<=.5) = -100

댓글 수: 4

Hi MattFig,
I tried the iteration approach
as written below:
x=rand(5,5);
T = zeros(size(x)); % Make another array to fill up...
for ii = 1:length(x)
if x(ii)>.5
T(ii) = 99;
else
T(ii) = -100;
end
end
but only the first column is filled up.
How do we extend to execute all columns.
Thank you
Emerson
LENGTH should only be used with vectors, as in my example. For a general array, use NUMEL instead of length.
for ii = 1:numel(x)
...
Thank you Mattfig,
that fixed the problem.
Wish you a nice day
Emerson
Mattfig,
What should be done if each individual element must be sorted through but there are thousands of elements. My script takes way too long to run. Is there any optimiziation?

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

추가 답변 (6개)

Rain
Rain 2013년 12월 11일
편집: madhan ravi 2019년 1월 15일
Hi,
Actually, there is a very simple way to do it:
X=rand(10,10);
X(X>0.5) = [-1];
Hope it is helpful.

댓글 수: 4

Hello Rain,
Thanks for the solution. lets consider different situation. For example if i have a 100*100 matrix of angles. I was to replace all the angle > 90 deg with 180-that angle. How can i acheive this ?
Thanks a lot
Hi,
I have a matrix as following and want to increase the value of numbers (by 1) that are less than 9.
B= [12,16,5,6,7,11,13,22]
I applied the below logic:
B(B<9)= [B(i)+1]
This is what I got:
B =
12 16 17 17 17 11 13 22
But, I look for a response like this:
B =
12 16 6 7 8 11 13 22
I appreciate it if you could help me.

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

You don;t need the if-statment here but only the logicals. This gives you a matrix with ones where X is larger than 0.5 and zeros other wise
(X>0.5)
To solve your problem
T = X.* (X<=0.5)-(X>0.5);
Alls values smaller than or equal to 0.5 are kept while the others are set to zero and then a matrix is added that has entries of -1 for the entries of X larger than 0.5.

댓글 수: 1

Hi Ivan
thank you for your help.
For this particular case you are right.
But how do we write, if the matrix T is build up
based on conditions of several matrices?
For example: The elements of T equals -1 if:
1) the elements of L>0.5 AND
2) the elements of M=0 AND
3) the elements of N<0.5
if any of these conditions is not satisfied, T equals the elements of M+1
Hope there is a way to do it
Thank you in advance
Emerson

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

Just implement it for multiple matrices using element multplication:
Logical=(L>0.5).*(M==0).*(N<0.5);
T=-Logical+(M+1).*Logical;

댓글 수: 3

Hi Ivan,
I tried the suggestion above, using 5x5 matrices L,M,N,but all elements of T equals zero. I guess we are making some mistakes.
Run the lines below and verify by yourself
clear all
close all
L=rand(5,5);
M=rand(5,5);
N=rand(5,5);
Logical=(L>0.5).*(M==0).*(N<0.5);
T=-Logical+(M+1).*Logical;
Thank you again for trying to help to solve this question
Emerson
Sorry, typo: it should be T=-Logical+(M+1).*(1-Logical); Ones and zeros.
That seems a little convoluted (I see how you're combining both assignments into one, but for a beginner the syntax might not be clear). This might be better:
T = M + 1;
T(L > 0.5 & M == 0 & N < 0.5) = -1;

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

yashar khatib shahidi
yashar khatib shahidi 2015년 5월 3일

0 개 추천

I have a row vector like a = [3 4 6 8 9] then I want to replace third element which is 6 with 5 and 8 so the new vector becomes b = [3 4 5 8 8 9]. What is the function to convert the vector. Thanks
Jyahway Dong
Jyahway Dong 2016년 10월 19일

0 개 추천

This is very important message for me, spend hours try to debug this and thank you all
Kinga Gyeltshen
Kinga Gyeltshen 2021년 3월 16일

0 개 추천

I have a 9x9 matrix and in every iteration i want to retain the 3x3 matrix and on to it I want to add the 4,5,6,7,8,9 (row, column) element to the 3x3 matrix to form 4x4 matrix. The fourth element (row,column) should get replaced with the remaining element of the 9x9 matrix. Can anyone help me with a simple and condensed algorithm to get it done please.
Thank you.

카테고리

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

제품

질문:

2011년 6월 5일

댓글:

2022년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by