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!