# Call elements for a cell array

82 ビュー (過去 30 日間)
Nikolas Spiliopoulos 2017 年 1 月 20 日

Hi there,
I am a beginner so the question maybe will be silly for you:
I have an cell 3x1 , where each element is an array
how can I select the first element, do some calculations and then the second one? (and so on..)
its a for loop but i cant find the proper syntax. thanks
Nikolas
##### 4 件のコメント表示非表示 3 件の古いコメント
Stephen23 2017 年 1 月 20 日

@Nikolas Spiliopoulos: That is a cell array containing all of your results.
What do you imagine should happen? What do you think MATLAB should do?
You stored the results in a cell array, and there they are, in a cell array. Exactly where you put them. My comments already showed you how to look at the values inside that cell array, if you want to do so.
Now that all of your results are stored in one cell array you could easily:
• combine the separate numeric arrays into one numeric array.
• loop over that output cell array and do further work: e.g. save the data, etc.
• pass that cell array to other operations or functions.
• etc
What happens next depends on your workflow. Which do you want to do?

サインインしてコメントする。

### 採用された回答

Stephen23 2017 年 1 月 20 日

C = {0:3, 4:5, 6:9};
out = NaN(size(C));
for k = 1:numel(C)
out(k) = mean(C{k});
end
or if the outputs are non-scalar, then put in a cell array:
out = cell(size(C));
for k = 1:numel(C)
out{k} = mean(C{k});
end
##### 7 件のコメント表示非表示 6 件の古いコメント
Stephen23 2017 年 1 月 20 日
@Nikolas Spiliopoulos: remember to accept the answer that helps you most.

サインインしてコメントする。

### その他の回答 (2 件)

Niels 2017 年 1 月 20 日

Hi,
>> a={[1 2] , 'abcdefg', randn(4)}
a =
1×3 cell array
[1×2 double] 'abcdefg' [4×4 double]
>> a{1}
ans =
1 2
or in a loop:
for i=1:3
b=a{i}; % you dont need to declare a new variable
% you can also work with a{i}
>> a{3}(1:2,2:3)
ans =
0.3188 3.5784
-1.3077 2.7694
end
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Nut 2017 年 1 月 20 日

Hi,
I think the following example could be a basic help, is it useful for you?
% 3x1 cell building (example)
cellarray{1} = [1 2 3];
cellarray{2} = [4 5 6];
cellarray{3} = [7 8 9];
cellarray = cellarray';
% Get the second array
cellarray{2}
% Get the first element of the third array
cellarray{3}(1)
% Get the first cell element
cellarray(1)
##### 2 件のコメント表示非表示 1 件の古いコメント
Nut 2017 年 1 月 20 日

I'm thinking to something like:
for i = 1:numel(cellarray)
array = cellarray{i};
% Do your calculations using "array"
end

サインインしてコメントする。

### カテゴリ

Find more on Matrices and Arrays in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by