필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

cell array concept issue

조회 수: 1 (최근 30일)
AKHILA GOUDA
AKHILA GOUDA 2019년 3월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
Sir I have an issue on cell array concept.
I want to generate 10 values using a for loop of 10 iteration and in every iteration it will give a single value.
I want to store all values in a single cell one by one value in every iteration.
Is it possible?
If Yes then please give a suggestion.

답변 (1개)

Stephan
Stephan 2019년 3월 3일
편집: Stephan 2019년 3월 3일
Hi,
in general no for loop is needed. See the following 2 examples how you could proceed in a simple example:
% 10 random values stored in a
a=randperm(10)
% every value of a in a single cell
b = num2cell(a)
% get the 5th entry of b
content_b_5 = b{5}
% get the whole content of b
content_b_all = cell2mat(b)
% all values of a in one cell as array
c = {a}
% get the 7th entry of c
content_c_7 = c{1}(7)
% get the whole content of c
content_c_all = c{:}
If you want to proceed in a for loop (which is usually not needed) you could use
% preallocate cell array
d = cell(1,numel(a));
% loop through
for k = 1:numel(a)
d{k} = a(k);
end
% show d
d
As you can see it is the same result as for creating b, but less efficient and more code to write.
Best regards
Stephan
  댓글 수: 2
madhan ravi
madhan ravi 2019년 3월 3일
content_c_all = c{:}
Stephan
Stephan 2019년 3월 3일
right you are - thanks for the hint

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by