matlab编程中5个数字中取最大数字和其位置

2025-03-23 14:01:01
推荐回答(2个)
回答1:

combntns(set,subset)
在集合set中取subset个元素的所有组合
例如:在[2 3 5 9 7]中取3个元素的所有组合为:
combntns([2 3 5 9 7],3)
运行结果:
2 3 5
2 3 9
2 3 7
2 5 9
2 5 7
2 9 7
3 5 9
3 5 7
3 9 7
5 9 7
matlab里要在1到310这310个数里取10个数,要求取遍每一种情况
可以设一个向量为
>> x=1:1:310;
A=combntns(x,10);

对于补充的问题
可以先设一个零矩阵
B=zeros(310,310);把对角线的元素存在一个向量y里面,对y赋值10个1,其他为0,然后取y所有的排列。

用命令perms得到排列,用法:
perms(vector)
给出向量vector的所有排列,例如
perms([2 3 5])
运行结果:
5 3 2
5 2 3
3 5 2
3 2 5
2 3 5
2 5 3
f馥骸zδ〓亭d.cg酢nd.zδ〓亭y颌k
可以参考参考·····

回答2:

int max ,max_num;
int a[5] = {4,5,8,9,3};
int i = 0;
max = a[0];
max_num = 1;
for( i = 1; i < 5; i ++)
if(a[i] > max){
max = a[i]
max_num = i+1;
}
printf( "%d %d" , max, max_num);
return 0;