[英]Naming index of array element with strings in matlab
Array elements: 数组元素:
4 3 2 1 5
a[1]=4
a[2]=3
a[3]=2
a[4]=1
a[5]=5
For Array index 1 corresponds to string name Basavaraj, 对于数组索引1对应于字符串名称Basavaraj,
index 2 corresponds to string name Chandru, 索引2对应于字符串名称Chandru,
index 3 corresponds to string name Natesh, 索引3对应于字符串名称Natesh,
index 4 corresponds to string name Vijay, 索引4对应于字符串名称Vijay,
index 5 corresponds to string name Raghu, 索引5对应于字符串名称Raghu,
So if array value 4 comes at index at 1 it has to display string Basavaraj, 因此,如果数组值4位于索引1处,则必须显示字符串Basavaraj,
3 comes at index 2 means string Chandru should be displayed, 3位于索引2处,表示应该显示字符串Chandru,
2 comes at index 3 means string Natesh should be displayed, 2位于索引3处,应显示字符串Natesh,
1 comes at index 4 means string Vijay should be displayed, 1位于索引4处,表示应该显示字符串Vijay,
5 comes at index 5 means string Raghu should be displayed, 5位于索引5表示应该显示字符串Raghu,
Sample of input array values: 输入数组值的样本:
4
4
4
3
3
3
1
1
1
2
2
2
5
5
5
output should be according to above array elements: 输出应根据上述数组元素:
Basavaraj
Basavaraj
Basavaraj
Chandru
Chandru
Chandru
Vijay
Vijay
Vijay
Natesh
Natesh
Natesh
Raghu
Raghu
Raghu
Sample of input array values: 输入数组值的样本:
5
5
5
1
1
1
2
2
2
4
4
4
3
3
3
output should be according to above array elements: 输出应根据上述数组元素:
Raghu
Raghu
Raghu
Vijay
Vijay
Vijay
Natesh
Natesh
Natesh
Basavraj
Basavraj
Basavraj
Chandru
Chandru
Chandru
Only 5 values are taken ie 1 2 3 4 5 How it can be done done in matlab? 仅采用5个值,即1 2 3 4 5如何在matlab中完成?
Alternate explanation: Consider below scenario: 替代解释:考虑以下情形:
Array:1 5 4 2 3 数组:1 5 4 2 3
ind[1]=1--->Basavraj IND [1] = 1 ---> Basavraj
ind[2]=5--->Chandru IND [2] = 5 ---> Chandru
ind[3]=4--->Natesh IND [3] = 4 ---> Natesh
ind[4]=2--->Vijay IND [4] = 2 --->维杰
ind[5]=3--->Raghu IND [5] = 3 ---> Raghu
Sample of input array: 输入数组的样本:
2
2
4
4
5
5
1
1
3
3
output: 输出:
Vijay
Vijay
Natesh
Natesh
Chandru
Chandru
Basavraj
Basavraj
Raghu
Raghu
You could use sth. 您可以使用某物。 like this:
像这样:
% 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}
I used rand
to get a random order in b
obviously you should use your vector as stated in your question. 我使用
rand
来获得b
的随机顺序,显然您应该使用问题中所述的向量。
Edit 1 Hey not so sure if this is what you asked (in comment): 编辑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)
Here List_Of_Names
is the list of your names. 这里的
List_Of_Names
是您的姓名列表。
input1
is the first input which defines the new order (idx(1)->5) not sure if this is what you are asking for. input1
是定义新顺序(idx(1)-> 5)的第一个输入,不确定这是否是您要的。
tmp_List_Names
is a temporal List which contains your names in the new order. tmp_List_Names
是一个临时列表,其中包含以新顺序排列的名称。
input2
is that long integer list which defines the output. input2
是定义输出的长整数列表。
result
is the output. result
就是输出。 And at the end I use display()
to show input2
and result
in the command window. 最后,我使用
display()
显示input2
并在命令窗口中显示result
。 Which would look like this: 看起来像这样:
input2 =
3 1 5 5 3 3 2 5 2 1
result =
'Vijay' 'Basavaraj' 'Natesh' 'Natesh' 'Vijay' 'Vijay' 'Raghu' 'Natesh' 'Raghu' 'Basavaraj'
If you want them to be in Columns not rows You can either transpose input2
and ( List_Of_Names
or result
). 如果希望它们位于“列”而不是“行”中,则可以转置
input2
和( List_Of_Names
或result
)。 Transpose is possible by adding '
: 通过添加
'
可以进行转置
result=result';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.