MATLAB: Comparing Elements of two matrices if loop

comparingif loop

Hi, I want to compare two matrices one with size m*n and the other 2*k. The second matrix (2*k) indicates values which need to be satisfied by the m*n matrix. Basically, I calculated a value in the second matrix for 360 degrees of a circle. In the first matrix measurements of these values for a circle. In the end I want to know how many times both values, the 360 degrees and the values are met.

Best Answer

Hi Esther, this is one of many ways to do this.
%Reshape A into 2-columns matrix (called Ar) to avoid having to use nested for loops later
Ar = reshape([A(:, 1:2:end) A(:, 2:2:end)], numel(A)/2, 2);
%R will store value = 1 if conditions are met, value = 0 if conditions fail
R = zeros(size(Ar, 1), 1);
for k = 1:size(Ar, 1)
%if azimuth and elevation in Ar is greater than ANY from B, change R(k) to 1
if any( Ar(k, 1) >= B(:, 1) & Ar(k, 2) >= B(:, 2))
R(k) = 1;
end
end
%Number of times conditions are met per hour (row) for all day (col)
R = reshape(R, size(A, 1), size(A, 2)/2);
R =
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
%Number of times conditions are met per day
Rday = sum(R, 1);
Rday =
0 0
%Number of times conditions are met per year
Ryear = sum(Rday);
Ryear =
0
Related Question