Matrix Data Results Explanation
1 次查看(过去 30 天)
显示 更早的评论
Augusto Gabriel da Costa Pereira
2023-9-15
评论: Augusto Gabriel da Costa Pereira
2023-9-15
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 个评论
采纳的回答
Fangjun Jiang
2023-9-15
编辑:Fangjun Jiang
2023-9-15
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!