help!! 求解matlab 的排序函数

来源:百度知道 编辑:UC知道 时间:2024/05/22 18:28:29
题目:a=[0,100,200,300],b=[19,18,16,17]经过排序后为a=[200,300,100,0],b=[16,17,18,19]
排序函数是sort
答案1 不符合要求

这么来:
clc;clear
a=[0,100,200,300],b=[19,18,16,17]
M=[a;b]
[M1,I]=sort(M(2,:))
M2=M(:,I)

结果:
a =

0 100 200 300

b =

19 18 16 17

M =

0 100 200 300
19 18 16 17

M1 =

16 17 18 19

I =

3 4 2 1

M2 =

200 300 100 0
16 17 18 19

楼上的没理解好楼主的意思.
应当是(以B排序顺序来调整A):

a=[0,100,200,300];
b=[19,18,16,17];
[bb,n]=sort(b);
aa=a(n);

%注这里用的是aa,bb,你也可直接改成
%[b,n]=sort(b);
%a=a(n)

a=sort(a,'descend')
t=a(2);
a(2)=a(1)
a(1)=t;
sort(b)
哪里不合要求?不能运行么?
a=sort(a,'descend')
a(1:2)=sort(a(1:2));