Hello, I have the following cell(data, 1x3):
data = [data{1} data{2} data{3}]
data{1} data{2} data{3}
A 15 1400
A 3 250
B 10 1100
C 13 1350
B 7 600
Now I would like to sort the data and make a new cell as follows:
newcell = [newcell{1} newcell{2} newcell{3}]
newcell{1} newcell{2} newcell{3}
A [15; 3] [1400; 250]
B [10; 7] [1100; 600]
C [13] [1350]
In other words, I want to sort and save the data of a cell with respect to A, B and C. Would you please show me how to do that with or without using for loop etc?
Thanks in advance.

댓글 수: 2

Geoff Hayes
Geoff Hayes 2015년 6월 17일
Yongmin - why don't you want to use a for loop?
Yongmin
Yongmin 2015년 6월 17일
편집: Yongmin 2015년 6월 17일
I guess the problem can be solved without the loop. If we find idx matrix describing the elements for A, B, and C, can we use the idx matrix for sorting data{2} and data{3}? But I don't know how to do it exactly.
If we use a for loop, would you please explain how to do it?
Thanks for your comment.

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2015년 6월 17일

0 개 추천

data = {'A' 15 1400; 'A' 3 250; 'B' 10 1100; 'C' 13 1350; 'B' 7 600}
[header, ~, row] = unique(data(:, 1));
catcol = @(col) arrayfun(@(r) [data{r == row, col}], 1:numel(header), 'UniformOutput', false)';
newcell = [header, catcol(2), catcol(3)]
would be one way to do it without a loop. It could be made even more generic with an additional arrayfun looping over the columns, but at this point, you'd be better off using a for loop to make it clearer.

댓글 수: 3

Yongmin
Yongmin 2015년 6월 17일
Thanks a lot for your explanation.
It seems to be too difficult to read. I agree with your opinion.
Would you please show your suggested code using for loop?
Guillaume
Guillaume 2015년 6월 17일
[header, ~, row] = unique(data(:, 1));
newcell = [header, cell(numel(header), size(data, 2)-1)];
for r = 1:numel(header)
for c = 2:size(data, 2)
newcell{r, c} = [data{r == row, c}];
end
end
Yongmin
Yongmin 2015년 6월 17일
Thanks a lot for your kind answer. It will help to develop codes for my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

태그

질문:

2015년 6월 17일

댓글:

2015년 6월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by