Hello all, I tried to sort a cell matrix, row by row with sortrows function but this sort all matrix instead of each row.
I would like to sort:
a=[{'c'},{'b'},{'a'};
{'c'},{'a'},{'b'};
{'c'},{'b'},{'a'};]
With this output:
a=[{'a'},{'b'},{'c'};
{'a'},{'b'},{'c'};
{'a'},{'b'},{'c'};]
Could someone help me?
Thank you!!!

2 件のコメント

Guillaume
Guillaume 2015 年 12 月 4 日
What a strange way to declare the cell array.
a = {'c', 'b', 'a';
'c', 'a', 'b';
'c', 'b', 'a'}
requires less typing. But if all cells are just a single character then using a char array:
a = ['cba'; 'cab'; 'cba']
is even simpler.
Stephen23
Stephen23 2015 年 12 月 5 日
編集済み: Stephen23 2015 年 12 月 5 日
Why the really strange cell array definition? It is much easier to define a cell array like this:
a = {'c', 'b', 'a'; 'c', 'a', 'b'; 'c', 'b', 'a'}

サインインしてコメントする。

回答 (3 件)

the cyclist
the cyclist 2015 年 12 月 4 日

2 投票

sort(a')'

5 件のコメント

the cyclist
the cyclist 2015 年 12 月 4 日
You need to do the awkward transpose-sort-transpose, because the sort command will only sort along the first dimension for a cell array.
Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 12 月 4 日
編集済み: Diego Makasevicius Barbosa 2015 年 12 月 4 日
Hi, I think this solution do not solve this problem, cause when I just sort(a')', it return a 1xn array sorted.
e.g:
a=[{'c'},{'b'},{'a'};
{'f'},{'d'},{'e'};
{'b'},{'c'},{'a'};]
sort(a')'
Outputs (1x9): 'a' 'a' 'b' 'b' 'c' 'c' 'd' 'e' 'f'
And I want (3x3):
'a' 'b' 'c'
'd' 'e' 'f'
'a' 'b' 'c'
Thank you!!!
the cyclist
the cyclist 2015 年 12 月 4 日
I get a 3x3 output for that case, as expected. Maybe double-check that the input was really 3x3 when you tested?
the cyclist
the cyclist 2015 年 12 月 4 日
To be clear, this was the exact code I ran:
a=[{'c'},{'b'},{'a'}; {'f'},{'d'},{'e'}; {'b'},{'c'},{'a'};];
sort(a')'
the cyclist
the cyclist 2015 年 12 月 5 日

As verified by Image Analyst and myself:

a = {'home', 'apple', 'tiger'; 'onion', 'house', 'knife'; 'sugar', 'money', 'rich'}
sorted_a = sort(a')'

yields, as expected:

a = 
    'home'     'apple'    'tiger'
    'onion'    'house'    'knife'
    'sugar'    'money'    'rich' 
sorted_a = 
    'apple'    'home'     'tiger'
    'house'    'knife'    'onion'
    'money'    'rich'     'sugar'

サインインしてコメントする。

Image Analyst
Image Analyst 2015 年 12 月 4 日

0 投票

Perhaps this:
a=[{'c'},{'b'},{'a'};
{'c'},{'a'},{'b'};
{'c'},{'b'},{'a'};]
charArray = cell2mat(a)
% Sort each row individually
for row = 1 : size(charArray, 1);
[~, sortOrder] = sort(charArray(row, :));
charArray(row,:) = charArray(row, sortOrder);
end
% Print to command window
charArray
The end is a character array, which is a heck of a lot easier to deal with than a cell array.

2 件のコメント

Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 12 月 4 日
Thank you Image Analyst! I programmed this way actually, but I would like to know if you have a simpler solution, without requiring the use of a loop.
Image Analyst
Image Analyst 2015 年 12 月 4 日
Guillaume has given two one-liners - one for cell arrays and one for character arrays. I suggest you avoid cell arrays unless you really need them (like for mixed data types or strings that are not all the same length), and in this situation you don't need cell arrays at all.

サインインしてコメントする。

Guillaume
Guillaume 2015 年 12 月 4 日

0 投票

a = {'c', 'b', 'a';
'c', 'a', 'b';
'c', 'b', 'a'}
num2cell(sort(cell2mat(a), 2))
is all that is needed. But as stated, if a was a char array to start with:
a = ['cba'; 'cab'; 'cba'];
sort(a, 2)
is so much simpler.

3 件のコメント

Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 12 月 4 日
Thank you for the answer Guillaume! But I think your solution does not work if instead of the cells are words (my case):
eg:
a = {'home', 'apple', 'tiger'; 'onion', 'house', 'knife'; 'sugar', 'money', 'rich'}
the cyclist
the cyclist 2015 年 12 月 4 日
Can someone besides me please verify that
sort(a')'
does work for this case as well? I'm baffled why Diego can't just use this very simple solution.
Image Analyst
Image Analyst 2015 年 12 月 4 日

Verfied:

a = {'home', 'apple', 'tiger'; 'onion', 'house', 'knife'; 'sugar', 'money', 'rich'}
sorted_a = sort(a')'
a = 
    'home'     'apple'    'tiger'
    'onion'    'house'    'knife'
    'sugar'    'money'    'rich' 
sorted_a = 
    'apple'    'home'     'tiger'
    'house'    'knife'    'onion'
    'money'    'rich'     'sugar'

サインインしてコメントする。

編集済み:

2015 年 12 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by