Check each element in matrix is less than value
20 views (last 30 days)
Show older comments
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 Comment
madhan ravi
on 6 Feb 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;
Answers (2)
John D'Errico
on 6 Feb 2019
Edited: John D'Errico
on 6 Feb 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.
0 Comments
Bob Thompson
on 6 Feb 2019
Edited: Bob Thompson
on 6 Feb 2019
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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!