Comparing Elements of two matrices if 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.

7 comentarios

José-Luis
José-Luis el 20 de Sept. de 2017
I don't get it. Are you looking for something like ismember()?
Esther Roosenbrand
Esther Roosenbrand el 20 de Sept. de 2017
So I want to know when shadow is generated at a point. So I have a matrix (m*n) with the sun position (azimuth and elevation, both in separate columns) for every hour of the day throughout the year, so it is a 24*730 matrix. Then I have a matrix (2*360) determined by the geometry of the buildings around the point, where the first column is the required sun altitude for 360 degrees. I want to determine the percentage of time both values conditions are met, so when the suns position is at the correct azimuth and with the correct elevation. Hopefully it's clearer now :)
OCDER
OCDER el 20 de Sept. de 2017
Editada: OCDER el 20 de Sept. de 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
Esther Roosenbrand el 21 de Sept. de 2017
so I have two matrixes, where A is suns position and B is 'minimal to be met' values. 1:2:end values of A are the azimuths and the 2:2:end are the elevations of each hour of the day being a separate row, so these are in two columns, but for 365 days of the year. This makes A a 24*730 matrix. The second matrix, B, is a 360*2 matrix, where for 360 degrees of the azimuth each elevation are in two separate columns. I want to determine how many times both the azimuth and elevation of matrix B are met by matrix A.
OCDER
OCDER el 21 de Sept. de 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.
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 el 21 de Sept. de 2017
Editada: Cedric el 21 de Sept. de 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?

Iniciar sesión para comentar.

 Respuesta aceptada

OCDER
OCDER el 21 de Sept. de 2017
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 comentarios

Esther Roosenbrand
Esther Roosenbrand el 22 de Sept. de 2017
Thanks so much! Exactly what I was looking for!
OCDER
OCDER el 22 de Sept. de 2017
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Cedric
Cedric el 21 de Sept. de 2017
Editada: Cedric el 21 de Sept. de 2017
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)))

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Sept. de 2017

Comentada:

el 22 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by