Comparing Elements of two matrices if loop

조회 수: 4 (최근 30일)
Esther Roosenbrand
Esther Roosenbrand 2017년 9월 20일
댓글: OCDER 2017년 9월 22일
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.
  댓글 수: 7
Esther Roosenbrand
Esther Roosenbrand 2017년 9월 21일
A=
-60 340 -60 340
-61 8 -61 8
-57 34 -57 34
-51 55 -51 55
-43 72 -42 71
-34 85 -33 85
-24 97 -24 97
-15 108 -15 108
-7 119 -7 119
1 130 1 130
7 143 7 143
12 156 12 156
So the first column here is the elevation angle and the second column is the corresponding azimuth for this angle. The third and forth follow the same pattern but for day 2. A is extended to 24*730 matrix.
B=
17 1
17 2
18 3
18 4
19 5
20 6
So the first column of b is the elevation and the second the azimuth required for the location. B is extended to a 360*2 matrix.
I want to determine how many times both the azimuth and elevation of matrix B are met by matrix A.
Cedric
Cedric 2017년 9월 21일
편집: Cedric 2017년 9월 21일
By counting, do you mean checking if there was one match?
For a given day you have on row of B, which is a single pair of elevation and azimuth. Then for the same day in A you have 24 pairs. How is it possible that you get twice (or more) the same pair of values from A when pair are given hourly?

댓글을 달려면 로그인하십시오.

채택된 답변

OCDER
OCDER 2017년 9월 21일
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
  댓글 수: 2
Esther Roosenbrand
Esther Roosenbrand 2017년 9월 22일
Thanks so much! Exactly what I was looking for!
OCDER
OCDER 2017년 9월 22일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cedric
Cedric 2017년 9월 21일
편집: Cedric 2017년 9월 21일
Or you can operate in 3D:
A = [-10, 2, -8, 4; ...
-4, 6, -3, 7; ...
2, 5, 3, 6] ;
B = [ 3, 6; ...
-8, 4] ;
dayHasMatch = squeeze(any(all(reshape(A, size(A, 1), 2, []) - permute(B, [3,2,1]) == 0, 2)))
with that you get
dayHasMatch =
2×1 logical array
0
1
which indicates that there was a match on day 2.
PS: and if you have an old version of MATLAB, the expansion must be performed using BSXFUN, and you can use a test of equality directly instead of checking that the difference is null:
dayHasMatch = squeeze(any(all(bsxfun(@eq, reshape(A, size(A, 1), 2, []), permute(B, [3,2,1])), 2)))

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by