how to convert column cell to row cell?

Hi,
For example I have a 3x1 cell matrix like this. {[1,2,3] [4,5,6] [7,8,9]}; where every element is a 1x3 matrix.
I want to convert the row cells to 3x1 column cells like this {[1;2;3] [4;5;6] [7;8;9]};
How do I do this ? Thank you.

1 Kommentar

jaweria kainat
jaweria kainat am 16 Jul. 2018
suppose i have 2 columns.how i can convert them into rows?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 26 Jan. 2017
Bearbeitet: Jan am 27 Jan. 2017

0 Stimmen

Or a loop:
for k = 1:numel(C)
C{k} = C{k}.';
end
[EDITED] Accroding to your comment:
M = [1 2 3; 4 5 6; 7 8 9];
C = mat2cell(M.', 3, [1,1,1]).';
{[1;2;3];
[4;5;6];
[7;8;9]}
Or again with a simple loop:
C = cell(3, 1);
for k = 1:3
C{k} = M(k, :).';
end

Weitere Antworten (1)

Guillaume
Guillaume am 26 Jan. 2017

3 Stimmen

cellfun(@transpose, yourcellarray, 'UniformOutput', false)

6 Kommentare

sumana
sumana am 26 Jan. 2017
Thank you,
I got this error with both the methods,
Error using .' Transpose on ND array is not defined. Error in call_load (line 23) F=cellfun(@transpose, F, 'UniformOutput', false);
My array looks like this. I want to replace the , with ;.
Guillaume
Guillaume am 26 Jan. 2017
Bearbeitet: Guillaume am 26 Jan. 2017
Clearly, from the error message, at least one of the element of the cell array is not a vector and has at least three dimensions. To find which one(s):
find(~cellfun(@ismatrix, yourcellarray))
sumana
sumana am 26 Jan. 2017
Thank you,
I ran that, and it returned all of the elements. How do I fix this ?
sumana
sumana am 26 Jan. 2017
Can you make a normal matrix like
[1 2 3
4 5 6
7 8 9]
to a cell matrix like this
{[1;2;3]
[4;5;6]
[7;8;9]} ?
Guillaume
Guillaume am 27 Jan. 2017
Bearbeitet: Guillaume am 27 Jan. 2017
" have a 3x1 cell matrix like this. {[1,2,3] [4,5,6] [7,8,9]}" and "I ran that, and it returned all of the elements". One of these two statements directly contradict the other:
>>c = {[1,2,3] [4,5,6] [7,8,9]};
>>find(~cellfun(@ismatrix, c))
ans =
1x0 empty double row vector
If the arrays in your cell arrays have more than two dimensions, there's no way to transpose them since transposition is only defined for 2D matrices. If what you want to do is just swap 1st and 2nd dimension, leaving the others untouced:
cellfun(@(m) permute(m, [2 1 3:ndims(m)]), yourcellarray, 'UniformOutput', false)
"Can you make a normal matrix [...] to a cell matrix" There's no such thing as a cell matrix. To produce the cell array in your example:
m = [1 2 3; 4 5 6; 7 8 9];
c = num2cell(m.', 2).'
sumana
sumana am 27 Jan. 2017
Thank you for explaining the dimensions. I guess my matrix had more than two dimensions.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 26 Jan. 2017

Kommentiert:

am 16 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by