Matrix Data Results Explanation
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Augusto Gabriel da Costa Pereira
el 15 de Sept. de 2023
Comentada: Augusto Gabriel da Costa Pereira
el 15 de Sept. de 2023
I have three 5x5 matrices attached here, Lat, Lon, and Data:
I am using the following code:
Lat = [-1 -1 -1 -1 -1;
-2 -2 -2 -2 -2;
-3 -3 -3 -3 -3;
-4 -4 -4 -4 -4;
-5 -5 -5 -5 -5];
Lon = [-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10];
Data = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20;
21 22 23 24 25];
intervaloLat = [-3.5, -1.5];
intervaloLon = [-35, -15];
latDentroDoIntervalo = Lat >= intervaloLat(1) & Lat <= intervaloLat(2);
lonDentroDoIntervalo = Lon >= intervaloLon(1) & Lon <= intervaloLon(2);
Resultado = Dados(latDentroDoIntervalo(:,1) & lonDentroDoIntervalo(1,:));
The result of Data should be 2x2: [8, 9; 13, 14]
And it should not be: [8; 13; 9; 14]
0 comentarios
Respuesta aceptada
Fangjun Jiang
el 15 de Sept. de 2023
Editada: Fangjun Jiang
el 15 de Sept. de 2023
The code looks good and shows good programming flow to use logical index. It will be very lengthy to explain why the result shows in that way. Maybe, if you replace your last line of code with the three lines below, the result would make more sense to you.
You can check the value of every variable after every step to make sense of the size and value of the variable.
Lat = [-1 -1 -1 -1 -1;
-2 -2 -2 -2 -2;
-3 -3 -3 -3 -3;
-4 -4 -4 -4 -4;
-5 -5 -5 -5 -5];
Lon = [-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10;
-50 -40 -30 -20 -10];
Data = [1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20;
21 22 23 24 25];
intervaloLat = [-3.5, -1.5];
intervaloLon = [-35, -15];
latDentroDoIntervalo = Lat >= intervaloLat(1) & Lat <= intervaloLat(2)
lonDentroDoIntervalo = Lon >= intervaloLon(1) & Lon <= intervaloLon(2)
index=latDentroDoIntervalo & lonDentroDoIntervalo
Resultado = zeros(size(Data))
Resultado(index)=Data(index)
Or, you can do it this way. This only applies when your "selected area" is "square".
RowIndex=find(all(latDentroDoIntervalo,2))
ColIndex=find(all(lonDentroDoIntervalo,1))
Resultado=Data(RowIndex,ColIndex)
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!