How to access data in a cell array?

조회 수: 3 (최근 30일)
Susan
Susan 2021년 2월 8일
댓글: Susan 2021년 2월 10일
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
samuel okeleye
samuel okeleye 2021년 2월 9일
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.
Susan
Susan 2021년 2월 9일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 9일
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
Walter Roberson
Walter Roberson 2021년 2월 10일
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?
Susan
Susan 2021년 2월 10일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by