Cell array indexing in matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
fereshteh beygi ahmadvandi
2021년 3월 14일
hi
I have an empty cell array
Let call it W
At the output It must have a cell array with two columns and n rows
Each element of columns is an array with variable length
I wanna write at the end of arrays in columns
How can access the last element of that arrays in the cells
for example W(1,2) end+1 i.e At the end of an array at the second column of first row
댓글 수: 0
채택된 답변
추가 답변 (1개)
Adam Danz
2021년 3월 16일
편집: Adam Danz
2021년 3월 16일
To access the last element of an array stored in cell array C at location {m,n},
y = C{m,n}(end);
To access the last element of all cells stored in cell array C,
y = cellfun(@(x)x(end),C);
Update
To append a value to the end of the vector stored in cell array C at location {m,n},
C{m,n}(end+1) = x;
To append a value M(m,n) in matrix M to the end of the vector stored in cell C{m,n},
C = reshape(arrayfun(@(i){[C{i}, M(i)]},1:numel(C)),size(C));
Demo:
C = reshape(arrayfun(@(i){rand(1,randi(100))},1:100),50,2); % original values
M = reshape(1:100,50,2); % values to append
C2 = reshape(arrayfun(@(i){[C{i}, M(i)]},1:numel(C)),size(C));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!