Calling multiple cell elements at once

Hi I have a 1x10000 cell that I want to take out elements from. In my example here I use a 1x2 cell where I want to take out the first value C{1,1} and C{1,2}, and save it in a vector for a summation in a later stage. How can I do this?
C = {{1, 2, 3; 11,22,33;111,222,333},{1, 2, 3; 11,22,33;111,222,333}}

댓글 수: 8

You mean
cellfun(@(x)x(1),C) % like this?
Stephen23
Stephen23 2019년 2월 15일
"I want to take out the first value C{1,1} and C{1,2}, ..."
Note that your example cell array contains nested cell arrays, so the first elements (they are not "values") of cell array C will also be cell arrays:
>> class(C{1,1})
ans = cell
>> size(C{1,1})
ans =
3 3
It is not clear how you expect to use those cell arrays for a "summation". Storing lots of scalar numerics in nested cell arrays is not a very efficient way to store numeric data.
Orongo
Orongo 2019년 2월 16일
I have done 10000 simulations of a 101x19 matrix and this is how I stored the results. Can consider changing it but not sure how or to what.
Orongo
Orongo 2019년 2월 16일
What does this do? cellfun(@(x)x(1),C) % like this?
madhan ravi
madhan ravi 2019년 2월 16일
It extracts the first value from all the cells.
Orongo
Orongo 2019년 2월 17일
편집: Orongo 2019년 2월 17일
This can be useful. In my qestion I mention a later stage, the stage is to count number of occurances. I have a matrix of 101x19 (same size as each cell) and want to count number of occurences of each value in each position. Let say the true answer is called matrix T and the simulated matrix S (a cell of dimension 1x10000), I want to count number of occurances of T(1,5) in S{1:10000}{1,5}.
Stephen23
Stephen23 2019년 2월 17일
Orongo's "Answer" moved her:
Thansk all for the inputs so far, I have submitted a new question covering this but also considering other aspects of my program. I hope you can help me there. You find it here
https://uk.mathworks.com/matlabcentral/answers/445374-finding-in-a-cell-array-within-a-cell-array
Stephen23
Stephen23 2019년 2월 17일
@Orongo:please add explanation to this thread, by adding comments.
And remember to accept the answer of someone who helped you, not your own "answer" which does not actually answer the question but is really just a comment.

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

답변 (2개)

Asieh Daneshi
Asieh Daneshi 2019년 2월 16일

0 개 추천

this is a 1*2 cell, which each element of it is a 3*3 matrix
you can call each element of the matrix using the following command
c{m,n}{p,q}
m and n are the column and row of the cell element, and p and q are the column and row of the matrix element.
you can use ":" to call multiple elements at once. for example: c{1,1}{1,1:3}

댓글 수: 4

Orongo
Orongo 2019년 2월 17일
This is helpful. I tried C{1,1:2}{2,3} and expected [33 33] but got an error. Is there a line of code to get [33 33] or do I have to do a for-loop?
Stephen23
Stephen23 2019년 2월 17일
편집: Stephen23 2019년 2월 17일
"this is a 1*2 cell, which each element of it is a 3*3 matrix"
The example shows a 1x2 cell array, each cell of which contains a 3x3 cell array.
"...I tried C{1,1:2}{2,3} and expected [33 33] but got an error"
The syntax C{1,1:2} generates a comma-separated list which you cannot append another set of curly braces onto when it contains multiple arguments. Read this to know more:
"...do I have to do a for-loop?"
Short answer: yes, even if implicitly inside a cellfun call, as madhan ravi showed earlier.
Long answer: you could concatenate the data into numeric arrays and then use simple and efficient indexing. Or avoid this whole situation by designing your data better: putting lots of scalar numerics into nested cell arrays forces you to write complicated code. One simple numeric matrix would make acessing that data trival and efficient.
Orongo
Orongo 2019년 2월 17일
I agree this is getting uneccesasy complicated and which I had saved the simulation better. The simulation takes long time to run, can I manipulate it anyhow to numeric arrays you mention?
Stephen23
Stephen23 2019년 2월 17일
편집: Stephen23 2019년 2월 17일
"The simulation takes long time to run, can I manipulate it anyhow to numeric arrays you mention?"
Most likely, yes.
But as you have not uploaded your functions or explained anything about the algorithm that you use, we have no idea what your simulation does. I recommend that you review the introductory tutorials and also the documentation on arrays and vectorized code:

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

Orongo
Orongo 2019년 2월 18일

0 개 추천

Ok. It seems like I have to do a for loop, there is no other way.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2019년 2월 15일

답변:

2019년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by