I need to put data from columns in different cells into one vector
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have 1x20 cell and from each I need to take 4th column and put all of them in one long vector in order from 1-20. Moreover, these 4th vectors does not have same length. Is there any possibility for loop or any other easier solution than manually do it?
Thanks, Martin
댓글 수: 0
답변 (2개)
Robert U
2018년 7월 9일
Hi Martin Pecha,
assuming your cells contain arrays of double:
% Create input cell array
cInput = cell(1,20);
cInput = cellfun(@(cIn) rand(randi([4,10],1),randi([1,100],1)),cInput,'UniformOutput',false);
% extract 4th column and concatenate vertically
cOutput = cell2mat(cellfun(@(cIn) cIn(:,4),cInput,'UniformOutput',false)');
Kind regards,
Robert
댓글 수: 0
Pawel Jastrzebski
2018년 7월 9일
Consider the following example:
% 1. Create fake data (you'll use the real data)
% create empty cell 1x20
c = cell(1,20)
% populate the cells 1 through 20 with a matrix
% that has 4 columns and random number of rows
for i=1:size(c,2)
rcol = 4;
rrow = randi([4 10]);
c{i} = rand(rrow,rcol);
end
% 2. Extract 4th column out of every cell and
% merge into vector
vector4 = [];
for i=1:size(c,2)
% access data in i-th cell
% and extract all values (:) from row 4
GetCellData = c{i}(:,4);
% append 'GetCellData' to an existing vector
vector4 = [vector4; GetCellData];
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!