数组元素:
4 3 2 1 5
a[1]=4
a[2]=3
a[3]=2
a[4]=1
a[5]=5
对于数组索引1对应于字符串名称Basavaraj,
索引2对应于字符串名称Chandru,
索引3对应于字符串名称Natesh,
索引4对应于字符串名称Vijay,
索引5对应于字符串名称Raghu,
因此,如果数组值4的索引为1,则它必须显示字符串Basavaraj,
3位于索引2处,表示应该显示字符串Chandru,
2位于索引3处,表示应显示字符串Natesh,
1位于索引4处,表示应该显示字符串Vijay,
5位于索引5处,表示应该显示字符串Raghu,
输入数组值的样本:
4
4
4
3
3
3
1
1
1
2
2
2
5
5
5
输出应根据上述数组元素:
Basavaraj
Basavaraj
Basavaraj
Chandru
Chandru
Chandru
Vijay
Vijay
Vijay
Natesh
Natesh
Natesh
Raghu
Raghu
Raghu
输入数组值的样本:
5
5
5
1
1
1
2
2
2
4
4
4
3
3
3
输出应根据上述数组元素:
Raghu
Raghu
Raghu
Vijay
Vijay
Vijay
Natesh
Natesh
Natesh
Basavraj
Basavraj
Basavraj
Chandru
Chandru
Chandru
仅采用5个值,即1 2 3 4 5如何在matlab中完成?
替代解释:考虑以下情形:
数组:1 5 4 2 3
ind [1] = 1-> Basavraj
ind [2] = 5 ---> Chandru
ind [3] = 4 ---> Natesh
ind [4] = 2 ---> Vijay
ind [5] = 3 ---> Raghu
输入数组的样本:
2
2
4
4
5
5
1
1
3
3
输出:
Vijay
Vijay
Natesh
Natesh
Chandru
Chandru
Basavraj
Basavraj
Raghu
Raghu
您可以使用sth。像这样:
% a is a cell array defined with {} containing each name in one element.
a={'Basavaraj', 'Chandru' ,'Natesh','Vijay','Raghu'};
%b is an array which has integer values from 1 to 5 (10 times) if you add more values in a b still generates random numbers for all elements (because of numel(a))
b= round((numel(a)-1)*rand(10,1)+1);
% this returns for each integer in b the according name from a (be careful to use {} to return the value, normal brackets won't work here
a{b}
我过去常常rand
得到随机订单,b
显然您应该按照问题中的说明使用向量。
编辑1嘿,不太确定这是否是您要求的(在注释中):
List_Of_Names={'Basavaraj', 'Chandru' ,'Natesh','Vijay','Raghu'};
input1 =[1 5 4 2 3];
tmp_List_Names = List_Of_Names(input1);
input2= round((numel(List_Of_Names)-1)*rand(1,10)+1);
result =tmp_List_Names(input2);
display(input2)
display(result)
这List_Of_Names
是您的名字列表。
input1
是定义新订单(idx(1)-> 5)的第一个输入,不确定是否是您要的。
tmp_List_Names
是一个临时列表,其中包含以新顺序排列的名称。
input2
是定义输出的长整数列表。
result
是输出。最后,我使用display()
来显示input2
和result
在命令窗口中。看起来像这样:
input2 =
3 1 5 5 3 3 2 5 2 1
result =
'Vijay' 'Basavaraj' 'Natesh' 'Natesh' 'Vijay' 'Vijay' 'Raghu' 'Natesh' 'Raghu' 'Basavaraj'
如果希望它们位于“列”而不是“行”中,则可以转置input2
和(List_Of_Names
或result
)。通过添加可以进行转置'
:
result=result';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句