MATLAB Answers

Adam
0

Storing data in nested for loop

Adam
さんによって質問されました 2017 年 10 月 30 日
最新アクティビティ Guillaume
さんによって コメントされました 2017 年 10 月 31 日
Dear all,
I have a cell let say :
cell_data= cell([400, 4)])
when I loop to perform some calculations: I go the the error "Subscripted assignment dimension mismatch."
I am doing the following:
for s=1:1:3
for k=1:1:4
for n=1:1:300;
zero_index=find(cell_data{s}(n, : , k)~=0); %%to find the index of values which are not zeros
nonZero_data=cell_data{s}(n, zero_index, k);
new_cell_data{s}(n,:, k)=nonZero_data
end
end
end
I got the error. Do you know why. Thanks...

  2 件のコメント

per isakson
2017 年 10 月 30 日
  • Hard to say, since you don't show what's in the cells of data_cell
  • Why use a cell array in the first place?
Adam
2017 年 10 月 30 日
More info. : I have three experiments (s=1:1:3) for each experiment I have four settings: k=1:1:4 for each setting there are 300 variables for each variable 400 data points are collected. I hope it is now clear...I solve it using taking into account the length of new non_zero data but the rest of the matrix is filled with new zeros!!

サインイン to comment.

1 件の回答

Guillaume
回答者: Guillaume
2017 年 10 月 30 日

This has nothing to do with the cell array.
Within each cell of your cell array, you have a 3D matrix. You are looping over the rows and pages of that 3D matrix and finding the non-zero elements in each column (by the way you don't need the ~=0, that's what find returns anyway). You are then putting the non-zero elements of the column into a new matrix. The problem is that for each column, you're not going to have the same numbers of non-zero elements in each columns. Hence you can't store them in a matrix.
You could potentially store them in cell arrays within your main cell array. But I suspect that a completely different approach would be better. What is the end goal?

  4 件のコメント

Guillaume
2017 年 10 月 30 日
Again, this has nothing to do with the cell array.
In each cell of the cell array, you have a matrix, e.g
m = [1 1 1
0 0 0
1 1 0]
You're finding the non-zeros elements in each row. In row1 it's [1 1 1], in row 2 it's [], in row 3 it's [1 1]. You are trying to construct a new matrix but each row has a different size. That is not possible.
The end goal is not to construct a new cell array. You obviously are going to do something with that new cell array. What is that?
edit: actually it's across the rows not columns but my explanation still stands
Adam
2017 年 10 月 30 日
At the end I want to calculate for example: the Mean value
for s=1:1:3
for k=1:1:4
for n=1:1:300;
Average(s,n,k)=mean(new_cell_data{s}(n,:, k))
end
end
end
Guillaume
2017 年 10 月 31 日
As I said you cannot construct a matrix that have rows of different size which is what will happen if you remove the 0s. What you can do is replace the 0s by NaNs and tell mean to ignore these NaNs.
Note that your k and n loops are completely unnecessary and only slow down your code. Learn to operate on whole matrices at once. In addition, since the matrices stored in your cell array have all the same size, you could just store them as a single higher dimension matrix which means you would need no loop at all.
Keeping it in a cell array (if matrices can be different size)
cell_average = cell(size(cell_data));
for cidx = 1:numel(cell_data))
cell_data_matrix = cell_data{cidx};
cell_data_matrix(cell_data_matrix == 0) = nan; %replace 0 by nan
cell_average = mean(cell_data_matrix, 2, 'omitnan'); %get mean across columns
end
Average = permute(cat(4, cell_average{:}), [4, 1, 3, 2]); %permute to get your s,n,k order
Storing it all in a higher dimension matrix:
data_matrix = cat(4, cell_data{:}); %store in higher dimension matrix;
data_matrix(data_matrix == 0) = nan; %replace 0 with nan
Average = permute(mean(data_matrix, 2, 'omitnan'), [4, 1, 3, 2]); %calculate mean and rearrange dimensions

サインイン to comment.



Translated by