How to replace the elements of a matrix using the conditions if,else?
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (6)
Rain
il 11 Dic 2013
Modificato: madhan ravi
il 15 Gen 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.
4 Commenti
Bineet_Mehra
il 20 Ago 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
Björn Kok
il 14 Mar 2018
X(X>90)=180
Mohammad Safayet Hossain
il 16 Feb 2019
Great command
Majid Mahmoodabadi
il 16 Apr 2022
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.
Ivan van der Kroon
il 5 Giu 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.
1 Commento
Emerson De Souza
il 5 Giu 2011
Ivan van der Kroon
il 5 Giu 2011
Just implement it for multiple matrices using element multplication:
Logical=(L>0.5).*(M==0).*(N<0.5);
T=-Logical+(M+1).*Logical;
3 Commenti
Emerson De Souza
il 5 Giu 2011
Ivan van der Kroon
il 6 Giu 2011
Sorry, typo: it should be T=-Logical+(M+1).*(1-Logical); Ones and zeros.
Kelly Kearney
il 11 Dic 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
il 3 Mag 2015
0 voti
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
il 19 Ott 2016
0 voti
This is very important message for me, spend hours try to debug this and thank you all
Kinga Gyeltshen
il 16 Mar 2021
0 voti
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.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!