简体   繁体   中英

Reshaping a 3d matlab matrix into 2d matrix with indices and values

I have a 3D matrix in MATLAB. It has 3 rows, 4 columns and 2 time frames. Please see the dataset:

>> size(filtered_data)
ans =
 3     4     2

>> filtered_data
filtered_data(:,:,1) =
 15     22     19     16
 15     15     13     17
 19     20     17     17

filtered_data(:,:,2) =
 14     17     14     10
 18     19     11     18
 16     15     14     17

I want to store all values of this 3D matrix with their indices into a 2 dimension variable.

This will look something like this

二维数据格式

I tried using the find() function, but it returns multiple indices and it requires you to enter a value for which you need to calculate the indices.

Is there a predefined MATLAB function for this problem?

I will appreciate any help.

I don't believe there is a builtin MATLAB function to do this, but it's easy enough to do yourself:

sz = size(filtered_data);
[x,y] = meshgrid(1:sz(2),1:sz(1));
output = [x(:).';y(:).';reshape(filtered_data(:),[],sz(3)).'];

Not much mystery to it. Its just a fact of reshaping your data and generating the indices from the sizes.

rows=repmat(1:size(filtered_data,1),1,size(filtered_data,2));
cols=repelem(1:size(filtered_data,2),size(filtered_data,1));
data_time_frame1=reshape(filtered_data(:,:,1),1,[]);
data_time_frame2=reshape(filtered_data(:,:,2),1,[]);

for a more flexible approach,

data_time_frame=reshape(filtered_data(:),size(filtered_data,3),[]);

Just fill a matrix with those operations. Also take some time to familiarize yourself with them, for future reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM