필터 지우기
필터 지우기

What is the problem in simple matrix operation.?

조회 수: 1 (최근 30일)
Nimisha
Nimisha 2014년 11월 23일
댓글: Azzi Abdelmalek 2014년 11월 23일
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
Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
편집: Azzi Abdelmalek 2014년 11월 23일
What is the error message?
Nimisha
Nimisha 2014년 11월 23일
Error using sort
DIM and MODE arguments not supported for cell arrays.
Error in print_Data (line 6)
[~,idx] = sort(in_st(:,2),'descend');
The above error message i am getting

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

채택된 답변

Guillaume
Guillaume 2014년 11월 23일
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
Nimisha
Nimisha 2014년 11월 23일
ITS true what you understand. In my program, i mean to say that, as a fisrt
op_st1
i want to ascend names, that is true in program, BUT in
op_st2
i want to descend marks, means second column..! Still not working.""
Guillaume
Guillaume 2014년 11월 23일
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');

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
편집: Azzi Abdelmalek 2014년 11월 23일
Use curly brackets { }
[~,idx] = sort(in_st{:,2},'descend')
  댓글 수: 3
Nimisha
Nimisha 2014년 11월 23일
Error using sort
Too many input arguments.
Error in print_Data (line 6)
[~,idx] = sort(in_st{:,2},'descend')
Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
[~,idx] = sort([in_st{:,2}],'descend')

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by