How can I transpose cells in a cell array
39 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
When I transpose a cell array, I get the correct result, but the individual cells appear as COLUMNS rather than as ROWS. Please see below and example;
temp =
'A30016' 'pre-employment medical examination'
'A52001' 'excision'
'A60001' 'test result(s)'
'H82013' 'benign positional vertigo'
'J99001' 'Minor Problem'
'J99004' 'Well Patient Care'
'R78007' 'lower respiratory tract infection'
'S52025' 'skin biopsy'
'S77008' 'basal cell carcinoma'
'W11002' 'oral contraceptive pill'
'X37001' 'pap smear'
>> a = cellfun(@transpose,temp,'UniformOutput',false); >> z=a'
z =
[ 6x1 char] [6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [ 6x1 char] [6x1 char]
[34x1 char] [8x1 char] [14x1 char] [25x1 char] [13x1 char] [17x1 char] [33x1 char] [11x1 char] [20x1 char] [23x1 char] [9x1 char]
>> z{1}
ans =
A
3
0
0
1
6
why is z{1} shown as a column? Thank you
0 comentarios
Respuestas (1)
the cyclist
el 4 de Sept. de 2016
I think you are getting yourself confused between operations on individual cells, and operations on the cell array.
To clarify ...
temp is an 11x2 cell array, and temp{1} -- the contents of its first cell -- is a 1x6 character array.
In your statement
a = cellfun(@transpose,temp,'UniformOutput',false);
you are telling MATLAB to look inside each cell of temp, and transpose its contents. But you are NOT telling MATLAB to transpose temp itself. Therefore, a is still an 11x2 cell array, but now a{1} is a 6x1 character array.
Finally, in your statement
z = a'
you are telling MATLAB to transpose a, the cell array, but NOT the contents of its cells. Therefore, z is a 2x11 cell array, and z{1} remains a 6x1 character array, just as a{1} was.
I hope that helps!
3 comentarios
the cyclist
el 4 de Sept. de 2016
Editada: the cyclist
el 4 de Sept. de 2016
You are mistaken that
a = cellfun(@transpose,temp,'UniformOutput',false);
converts temp to a 2x11 array. You can prove this to yourself using the command
size(a)
You will find that a is 11x2, just like temp is.
What that line of code does, as I explained in my answer, is transpose the contents of each cell. The cellfun function operates on the contents of each cell. That is why the contents a{1}, and also z{1}, is the column array
z{1}
ans =
A
3
0
0
1
6
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!