I have an array of 69 x 1 cells. All cells are either 12 x 2 or 13 x 2 matrices. I want to make a matrix, with all second columns from the matrices in the array.
So for example:
This is my array: (I havent given names to the cells yet, but just for the example)
[y1; y2; y3; y4; y5.......]
y1 = 12 x 2 matrix y2 = 12 x 2 matrix y3 = 13 x 2 matrix y4 = 12 x 2 matrix y5 = 13 x 2 matrix
I want all second columns from y1 till y69 in a n x 69 matrix.
Thanks for the help!

2 commentaires

Stephen23
Stephen23 le 28 Sep 2015
Modifié(e) : Stephen23 le 28 Sep 2015
Some of the matrices have twelve rows, some have thirteen. How do you want their columns to be merged into one matrix: do you want to trim the longer columns, or pad the shorter ones? What value should be used for padding? Leading or trailing padding?
Royvg94
Royvg94 le 28 Sep 2015
Oh sure, forgot about that, i want to trim the longer ones.

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 28 Sep 2015

0 votes

You're mixing up terms and notations, so it's not clear what you have and want. My understanding it that you have a cell array (size 69 x 1) consisting of matrices (size N x 2). The notation for cell arrays uses {}:
carr = {y1; y2; y3; ...} %yn can all be of different size and even type
Because the number of rows in each of these matrices is different (either 12 or 13), it's not possible to combine these seconds column into a matrix. It is however possible to combine them into a cell array. This is easily done with cellfun:
carr = {rand(12, 2); rand(13, 2); rand(12, 2); rand(13, 2)}; %demo data
column2 = cellfun(@(m) m(:, 2), carr, 'UniformOutput', false);
If cellfun is too complex for you, you can use a loop. The cellfun above is exactly equivalent to:
column2 = cell(size(carr));
for cidx = 1:numel(carr)
column2{cidx} = carr{cidx}(:, 2);
end

5 commentaires

With the trimming you can put it all together in one array:
column2 = cellfun(@(m) m(1:12, 2), carr, 'UniformOutput', false);
output = horzcat(column2{:});
Royvg94
Royvg94 le 28 Sep 2015
Where do i have to put the name of the array in this formula?
Guillaume
Guillaume le 28 Sep 2015
Modifié(e) : Guillaume le 28 Sep 2015
carr is the name of the input cell array, as per the comment, %demo data, in my examples.
Indeed, if you want to trim the matrices, use 1:12 instead of : in either example and then horzcat the cell array, as per Walter's comment.
Royvg94
Royvg94 le 28 Sep 2015
Do you also know how i can fill everything until 13 cells by adding zero's ?
The principle is still the same, use cellfun or a loop to extract and do whatever you want with each column. In this case, simply add 13-number of rows 0 to the column:
column2 = cellfun(@(m) [m(:, 2); zeros(13-size(m,1), 1)], carr, 'UniformOutput', false);
column2 = [column2{:}]

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 28 Sep 2015

0 votes

x = arrayfun(@(x)randi(56,randi([12 13]),2),(1:69)','un',0); % x - your array
n = cellfun(@(x)size(x,1),x);
nm = max(n);
m = numel(x);
out = nan(nm,numel(x));
for ii = 1:m
out(1:n(ii),ii) = x{ii}(:,2);
end

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by