Reputation:
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.
Upvotes: 0
Views: 112
Reputation: 35525
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
Upvotes: 1
Reputation: 4768
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)).'];
Upvotes: 3