How to access data in a cell array?

Hi,
I have an 40*8 cell array, called Q. Each cell contains an 64×64×128 double matrix.
I would like to have a foor loop on the 3rd dimension of each matrix in these cells. How can I do that? Thanks!

2 Comments

Hello Susanna,
What do you want the for loop to do? It will be easier to help you if you specify what you want the for loop to do.
Hi Samuel,
Thanks for your reply and sorry that I didn't clarify what exactly I want the for loop to do for me. I tried to explain it below. Any feedback would be greatly appreciated.

Sign in to comment.

Answers (1)

mask = ~cellfun(@isempty, Q);
results = cell(size(Q));
results(mask) = cellfun(@(C) DO_SOMETHING_3D(C), Q(mask), 'uniform', 0);
For example,
mask = ~cellfun(@isempty, Q);
results = cell(size(Q));
results(mask) = cellfun(@(C) sum(C, 3), Q(mask), 'uniform', 0);

5 Comments

Thank you so much for your response, Walter. This is what I want to do
I would like to have a for loop over the 3rd dimension, say in each cell that I have an 64*64*128 matrix I want to do something on each matrix (:,:,k) as follows
Q{1,1} = is an 64*64*128 matrix. Let's call it X
for each cell
for k = 1 : 128
DO_SOMETHING_3D = Do something on X(:,:,k) ---> output would be a matrix for this specific k
end
end
In the Output I would like to have a same size cell as Q, i.e., 40*8, that in each cell I have 128 (the number of 3rd dimension) matrices.
So, my question is how can I define a function "DO_SOMETHING_3D(C)" in the for loop k/contains the for loop. I think if I can define "DO_SOMETHING_3D(C)" in such a way that
function[output]=DO_SOMETHING_3D(Q)
for k = 1 : 128
Do something --> For calculation here I need to have access to the matrices in all other cells
end
end
then I can easily apply your code and get whatever I want. Could you please help me out with this? Many thanks in advance!
Per_slice_operation = @(M) conv2(M, [1 0 1;0 -4 0; 1 0 1], 'same'); %for example
DO_SOMETHING_3D = @(M) arrayfun(@(S) Per_slice_operation(M(:,:,S)), 1:size(S,3), 'uniform', 0);
mask = ~cellfun(@isempty, Q);
results = cell(size(Q));
results(mask) = cellfun(@(C) DO_SOMETHING_3D(C), Q(mask), 'uniform', 0);
However
Do something --> For calculation here I need to have access to the matrices in all other cells
I am not sure there what you mean when talking about all the other cells. Are you talking about needing access to all of the other Q cells when calculating for any one Q cell? If so then I will need more information about what you are doing.
Hi Walter,
Thanks for your reply.
Yes, you are rigt. I am talking about needing access to all of the other cells when calculating for any one Q cell. In fact the function that I am interested in calculating it at each k = 1: 128 is a function of the matrices in all cells. Does it make sense?
Does your calculation need to know which is the current cell index, in order to know what it is calculating?
Or is it a matter that the content of the current slice is needed along with the mass information about all the other slices at the same depth, without needing to know anything about the position? For example some kind of correlation?
There could be a hybrid situation that might or might not lead to efficiencies, such as if you needed to do cross-correlation against all the other slices at the same depth.
This calculation that is being done that needs to know all of the other slices at the same depth: is there a constant value that is mathematically neutral or ignored when seen? What I am leading up to here is that sometimes if you need access to mass information, the easiest approach is to concatenate all of that information together into a large matrix, and if you do that you need to consider the effect of the empty slices: do you just mass together the data that is there without considering position, or do you put in filler values such as 0 or nan that (somehow) will not affect the calculation?
If knowing the position is important to the calculation, then would putting everything together into 64 x 64 x 128 x (40*8) work, or would it need to be 64 x 64 x 128 x 40 x 8 to get the calculations right?
Thanks again for your detailed reply.
Yes, my calculation needs to know which is the current cell index in order to know what it is calculating and I think putting everything together would need to be 64 x 64 x 128 x 40 x 8 since the output should be an 40 x 8 cell array that each array contains a 64 x 64 x128 matrix.

Sign in to comment.

Categories

Asked:

on 8 Feb 2021

Commented:

on 10 Feb 2021

Community Treasure Hunt

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

Start Hunting!