1,528 views (last 30 days)

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
on 5 Jun 2011

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

Matt Fig
on 5 Jun 2011

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)

...

Nicholas Nurre
on 30 Oct 2019

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?

Rain
on 11 Dec 2013

Edited: madhan ravi
on 15 Jan 2019

Hi,

Actually, there is a very simple way to do it:

X=rand(10,10);

X(X>0.5) = [-1];

Hope it is helpful.

Bineet_Mehra
on 20 Aug 2016

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

Ivan van der Kroon
on 5 Jun 2011

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.

Ivan van der Kroon
on 5 Jun 2011

Just implement it for multiple matrices using element multplication:

Logical=(L>0.5).*(M==0).*(N<0.5);

T=-Logical+(M+1).*Logical;

Ivan van der Kroon
on 6 Jun 2011

Sorry, typo: it should be T=-Logical+(M+1).*(1-Logical); Ones and zeros.

Kelly Kearney
on 11 Dec 2013

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
on 3 May 2015

Jyahway Dong
on 19 Oct 2016

This is very important message for me, spend hours try to debug this and thank you all

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.