Find values in 3D matrix with given indices
4 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I'm trying to modify my matrix but I'm a little stuck. Maybe someone has an idea how it could be solved in a less complicated way :)
Basically, I have a 3D data set whereis the first two dimension represent coordinates, longitude and latitude and the third one is the time. The data is given for a set of coordinates in a range where longitude is from -180:180 and the latitude from 20:90. Now I want to change the data so it is not given for all coordinates but just section where the latitude is from 77:81 and the longitude is 0 with +-1 entry in the matrix. In the end my data should have the dimensions 5x3x48.
I tried to do it in a way that I find the indexes where in the grid data these values take place, but i don't know how do i search and define my data vector with these given indices.
Maybe someone has an Idea, I would really appreciate it.
Thank you for your time :)
Bianka
%3D data
data=rand(71,361,48);
%Longitude and latitude data
lon_oras=(-180:180);
lat_oras=(20:90);
%define longitude section
lon=find(lon_oras==0);
%for more robustness
po=lon+1;
ne=lon-1;
%index fof longitude
idx_lon=[ne lon po];
%define latitude section
lat_range=77:81;
%find the index where the matrix take the numbers written in lat_range
idx_lat = find(lat_oras>=min(lat_range) & lat_oras<=max(lat_range));
0 个评论
采纳的回答
Scott MacKenzie
2021-7-20
编辑:Scott MacKenzie
2021-7-20
Unless I'm missing something, this is just a simple matter of finding the indices corresponding to the latitude 77:81 elements and the longitude -1:1 elements. Try this:
data=rand(71,361,48); % lat (20:90), long (-180:180), time
% find indices to extract subset of data
latIdx = (77:81) - 19; % remap so 20 is 1, 21 is 2, and so on
lonIdx = (-1:1) + 181; % remap so -180 is 1, -179 is 2, and so on
datanew = data(latIdx,lonIdx,:);
whos
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!