Comparing Elements of two matrices if loop
Show older comments
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 Comments
José-Luis
on 20 Sep 2017
I don't get it. Are you looking for something like ismember()?
Esther Roosenbrand
on 20 Sep 2017
Hi Esther, we want to help but it's still confusing. In MATLAB, m*n means m rows by n columns. You have azimuth + elevation in "separate columns" (2 columns) for 24 hrs (24 rows) for 365 days (but there're 730 columns?) Did you mean a (24*365) row by 2 column matrix?
Then you have 2*360 matrix, which Matlab users interpret as 2 rows by 360 columns, where the "first column" (2 data points) is the "required sun altitude for 360 degrees" (360 data points). Did you mean 360x2 matrix? Also, what does the second column store?
Lastly, about the sun being at a correct azimuth and elevation, I'm not sure what is "correct". Must they have the exact, minimum, maximum value of degree and elevation?
Since you are comparing two matrices, there is a MATLAB function called intersect that lets you find the location and values where 1st matrix row equals 2nd matrix row. Is this what you need? Ex:
[AB, Aidx, Bidx] = intersect(A, B, 'rows')
AB = matrix that stores rows of A and B that are the same (intersect)
Aidx = row index in A that's the same as AB ( AB = A(Aidx, :) )
Bidx = row index in B that's the same as AB ( AB = B(Bidx, :) )
Esther Roosenbrand
on 21 Sep 2017
OCDER
on 21 Sep 2017
Okay, things are making more sense now. Lastly, can you provide a small sample of A (perhaps 6 by 2 matrix), B (perhaps 12 by 2 matrix), and your desired/expected results for that example? It's much easier for us to start with a data to reach your objective, as opposed to providing an answer solely based on our interpretation.
Esther Roosenbrand
on 21 Sep 2017
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?
Accepted Answer
More Answers (1)
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)))
Categories
Find more on Matrices and Arrays 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!