Check each element in matrix is less than value
Afficher commentaires plus anciens
i have two 20 by 20 matrix, T and Q.
i would like to check if each element of T and Q is less than, more than or equal to zero. For each combination there is a condition for Beta. For example, if it is found that T(1,1)>0 and Q(1,1)>0, Beta=-90 if it is found that T(1,2)<0 and Q(1,2)>0, Beta=90 and so on until T(20,20) and Q(20,20)
1 commentaire
madhan ravi
le 6 Fév 2019
Does this do what you want?
Beta = zeros(size(T));
id = T>0 & Q>0;
Beta(id) = -90;
id1 = T<0 & Q>0;
Beta(id1) = 90;
Réponses (2)
John D'Errico
le 6 Fév 2019
Modifié(e) : John D'Errico
le 6 Fév 2019
Learn to use MATLAB, as it is designed to be used. For example, you might just do this:
Beta = -90*sign(T).*sign(Q);
You don't say what happens if either of T or Q are zero, or what to do if they are both negative. But the above line computes Beta(i,j) all in advance, without requiring a complex set of if statements.
Oh, I usually tell people to avoid using names of variables that are too close to useful function names. Here, beta is a function in MATLAB. Since MATLAB is case sensitive in this respect, this is not a problem in theory, but it is something to watch out for.
Bob Thompson
le 6 Fév 2019
Modifié(e) : Bob Thompson
le 6 Fév 2019
0 votes
i would like to check if each element
By 'check each element' do you actually need to go through each element in a loop, or would creating a beta array with appropriate values work?
Beta = nan(20,20);
Beta(T>0&Q>0) = -90;
Beta(T<0&Q>0) = 90;
... % Other possibilities
Catégories
En savoir plus sur Naming Conventions dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!