How can I transpose cells in a cell array

44 Ansichten (letzte 30 Tage)
Branko Celler
Branko Celler am 4 Sep. 2016
Bearbeitet: the cyclist am 4 Sep. 2016
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

Antworten (1)

the cyclist
the cyclist am 4 Sep. 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 Kommentare
Branko Celler
Branko Celler am 4 Sep. 2016
That can be seen from the fact that temp is converted from a 11x2 cell array to an 11x2 cell array. But note that individual cells are now 6x1 character elements ie in columns, not rows. Similarly the second row is made up of variable length character array organised in column form not row form.
the cyclist
the cyclist am 4 Sep. 2016
Bearbeitet: the cyclist am 4 Sep. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by