Converting 1D indexed array to 2D matrix

33 ビュー (過去 30 日間)
John Cruce
John Cruce 2021 年 10 月 3 日
コメント済み: dpb 2021 年 10 月 4 日
I have a 1-D array of radar data for latitude, longitude, and altitude (dimensions of 2301 x 1201 x 24). Since not all latitude, longitude, and altitudes contain measurable reflectivities (radar data), the radar data are stored as an indexed 1-D array to save storage space. (If all latitude, longitudes, and altitudes contained data, this 1-D array would span 66,324,024 (2301x1201x24) cells).
Indices are zero-based, with data stored in order of increasing Longitude, then Latitude, then Altitude. So for example, the southwest corner of the grid at the lowest altitude is index 0. The southeast corner of the grid at the lowest altitude is index 2300.
I need to get this 1-D array over to a 2-D array based on the first altitude level (or alternatively on a 3D array for lattitude, longitude, and altitude). I start with converting linear indices to subscripts, but once I do so, how do I broadcast the indexed-based reflectivities on a 2D array of longitude and latitude?
[lonind,latind,altind] = ind2sub([2301 1201 24],inddata);

採用された回答

dpb
dpb 2021 年 10 月 4 日
編集済み: dpb 2021 年 10 月 4 日
You don't need to convert the linear addresses to indices to assign them to the array. If you have, indeed, segregated the altitudes into subsections of vectors of 2300 (or 2301?) elements each, then to create a 2D plane for each elevation is straightforward--
P=zeros(2301,1201); % a 2D plane array
[r,c,p]=ind2sub([2301 1201 24],inddata(1:2301)); % the first altitude plane --> 1:2301
P(sub2ind(size(P),r,c))=R(1:2301); % assign first altitude Radar data to those positions
You can loop through the full array in groups of 2301 for each altitude plane or compute the locations within the vector based on the number.
NB: If, indeed, the indices stored are 0-based, you'll need to add one to the inddata array above to match up to the 1-based array indexing in MATLAB. If you have control over computing those indices, it would probably make sense to make the fix up at that point unless the same data are being used in C-based code or somesuch.
To handle a subset as 3D array, just build a 3D array of 1:nPlanes wish to handle and do the same thing except pull as many segments of 2301 indices as need for the number of planes, not just one.
ADDENDUM:
Note that you can subtract ElevationLevel*NumElementsPerElevation from each vector of NumElementsPerElevation indices to use as indices into the location of a given 2D plane--the elements are stored linearly by row, then column and then by plane.
That lets you generalize the linear addressing into the plane by referring them all back to as if were the r,c indices of the first plane. Doing that could eliminate the ind2sub call entirely.
  2 件のコメント
John Cruce
John Cruce 2021 年 10 月 4 日
Brilliant! Thank you!
dpb
dpb 2021 年 10 月 4 日
Glad to help...indeed, often for similar things the linear addresses are the ticket--one thing if get a list of [r,c,p] indices from ind2sub, then you have to use an array-looping structure to put those into an array indexing expression as MATLAB will expand the lists by combination instead of processing each as a single element from each vector. I don't fully comprehend the why behind that decision; I can't think of a time it was the behavior I've wanted but I suppose there is some specific case in which it is and that was what controlled the decision. Or, maybe it was just done that way "in the beginning" and remains because of compatibility.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Clutter についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by