I need to put data from columns in different cells into one vector

12 ビュー (過去 30 日間)
Martin Pecha
Martin Pecha 2018 年 7 月 9 日
回答済み: Pawel Jastrzebski 2018 年 7 月 9 日
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
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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by