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

回答(2 个)

Robert U
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

Pawel Jastrzebski
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

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by