How to append a vector to a cell array?

How do you append a vector to an existing cell array? Each element of the latter contains a vector with double precision. I want to append vectors as new elements in the cell array.

 채택된 답변

Jan
Jan 2022년 3월 29일
편집: Jan 2022년 3월 29일

2 개 추천

C = {[1,4,1], 0:10}; % The cell
v = linspace(1, 10, 100); % The vector
C{end + 1} = v;
% Or:
C{numel(C) + 1} = v;
% Or slower and less elegant:
C = cat(2, C, {v})

댓글 수: 3

L'O.G.
L'O.G. 2022년 3월 29일
편집: L'O.G. 2022년 3월 29일
@Jan Excellent! Thank you! Just one thing -- how would you do it for multiple vectors? Each vector would be a different length, so concatenating them into one matrix I don't think is possible.
Jan
Jan 2022년 3월 29일
편집: Jan 2022년 3월 29일
In exactly the shown way:
C = {[1,4,1], 0:10}; % The cell
v = linspace(1, 10, 100); % The first vector
w = rand(1, 17); % The second vector
... % Equivalent for more vectors
C{end + 1} = v;
C{end + 1} = w;
% Or:
C = cat(2, C, {v, w})
% Equivalent:
nC = numel(C)
C(nC + 1:nC + 2) = {v, w};
L'O.G.
L'O.G. 2022년 3월 29일
Thank you!

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

추가 답변 (1개)

Voss
Voss 2022년 3월 29일

1 개 추천

% A cell array of vectors, C:
C = {[1 2 3]; [1 2 3 4 5]; [1; 2; 3; 4; 5; 6]}
C = 3×1 cell array
{[ 1 2 3]} {[1 2 3 4 5]} {6×1 double }
% Append a new vector to the end of C:
new_vector = 1:10;
C{end+1} = new_vector
C = 4×1 cell array
{[ 1 2 3]} {[ 1 2 3 4 5]} {6×1 double } {[1 2 3 4 5 6 7 8 9 10]}

카테고리

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

제품

릴리스

R2021b

질문:

2022년 3월 29일

댓글:

2022년 3월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by