What is the problem in simple matrix operation.?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nimisha
am 23 Nov. 2014
Kommentiert: Azzi Abdelmalek
am 23 Nov. 2014
clear all;clc
names = {'A'; 'B'; 'C'; 'D'};
marks = [27; 48; 84; 22];
in_st = {names marks};
in_st = [names num2cell(marks)];
function [op_st1,op_st2] = print_Data(in_st)
in_st = in_st;
[~,idx] = sort(in_st(:,1));
op_st1 = in_st(idx,:)
[~,idx] = sort(in_st(:,2),'descend');
op_st2 = in_st(idx,:)
I want op_st1 in ascending order, and op_st2 in descending order. It works well upto half program, what is the problem in remaining..?
2 Kommentare
Azzi Abdelmalek
am 23 Nov. 2014
Bearbeitet: Azzi Abdelmalek
am 23 Nov. 2014
What is the error message?
Akzeptierte Antwort
Guillaume
am 23 Nov. 2014
As the error message says and the documentation of sort states, you can't use the DIM and MODE arguments when sorting cell arrays.
However, since the descending order is just the reverse of the ascending order, why don't you just flip your first sort?
[~, idx] = sort(in_st(:, 1));
op_st1 = in_st(idx, :);
op_st2 = in_st(flipud(idx), :);
2 Kommentare
Guillaume
am 23 Nov. 2014
Oh, yes, sorry, I missed that. You can still reverse do what I said but on column 2:
[~, idx] = sort(in_st(:, 2));
op_st2 = in_st(flipud(indx), :);
Or you could convert column 2 to matrix:
[~, idx] = sort(cell2mat(in_st(:, 2)), 'descend');
Weitere Antworten (1)
Azzi Abdelmalek
am 23 Nov. 2014
Bearbeitet: Azzi Abdelmalek
am 23 Nov. 2014
Use curly brackets { }
[~,idx] = sort(in_st{:,2},'descend')
3 Kommentare
Siehe auch
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!