数组元素:
4 3 2 1 5
a[1]=4
a[2]=3
a[3]=2
a[4]=1
a[5]=5
对于Array索引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,5,如何在matlab中实现?
替代解释:考虑以下场景:
阵列:1 5 4 2 3
Ind1=1-->Basavraj
Ind2=5-->Chandru
Ind3=4-->Natesh
Ind4=2->Vijay
Ind5=3-->Raghu
输入数组的样本:
2
2
4
4
5
5
1
1
3
3
产出:
Vijay
Vijay
Natesh
Natesh
Chandru
Chandru
Basavraj
Basavraj
Raghu
Raghu
发布于 2014-06-12 04:26:45
你可以用某物。就像这样:
% 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';
https://stackoverflow.com/questions/24183778
复制