2014-06-12 63 views
0

数组元素:在MATLAB命名数组元素的索引与串

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意味着串维杰应当被显示,

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 --->维杰

IND [5] = 3 ---> Raghu

输入阵列的样品:

2 
2 
4 
4 
5 
5 
1 
1 
3 
3 

输出:

Vijay 
Vijay 
Natesh 
Natesh 
Chandru 
Chandru 
Basavraj 
Basavraj 
Raghu 
Raghu 

回答

1

你可以使用某事物。像这样:

% 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} 

我用randb获得一个随机的顺序很显然,你应该用你的载体作为您的问题声明。

编辑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()在命令窗口中显示input2result。这将是这样的:

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_Namesresult)。移调可以通过添加'

result=result'; 
+0

我的问题是有些不同的,请检查我的另一种解释 – prashanth

+1

@prashanth我编辑我的职务。检查是否现在你想要的结果。 –