0

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

2-dimensional data format

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.

Wolfie
  • 27,562
  • 7
  • 28
  • 55

2 Answers2

3

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)).'];
MrAzzaman
  • 4,734
  • 12
  • 25
  • 1
    That's true. I never deal with complex data, so I've adopted the bad habit of just `'` instead of `.'` out of laziness. I've edited the answer to fix this. – MrAzzaman Jun 25 '18 at 14:04
  • Actually my dataset is large, how can I make this code dynamic. So that it can run for i*j*k dimensional dataset –  Jun 25 '18 at 14:09
  • 1
    I modified the answer to remove the explicit dependence on the size of the data – MrAzzaman Jun 25 '18 at 14:11
1

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

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Do I need to hard code my time frame values? I want to run this code for `n` time frames. How can I make this code dynamic? –  Jun 25 '18 at 14:06