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));

采纳的回答

Scott MacKenzie
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
Name Size Bytes Class Attributes data 71x361x48 9842304 double datanew 5x3x48 5760 double latIdx 1x5 40 double lonIdx 1x3 24 double

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by