简体   繁体   English

获取与向量中的字符串匹配的对应字符串的索引

[英]get index for corresponding string matching a string within a vector

I have a vector of different string name; 我有一个带有不同字符串名称的向量;

A = {

    'alex'
    'alex'
    'sophie'
    'alex'
    'david'
    'sophie'
    'david'
    'david'
    'sophie'
    'alex' };

and for which correspond 2 variables, lets say age and size type_age = [1:10]; 并为其对应2个变量,假设年龄和大小type_age = [1:10]; type_size = [10:10:100]; type_size = [10:10:100];

I want to get the be able to do something like 我希望能够做类似的事情

 un_a = unique(A);
 f = find(A==un_a(1)); % I know this would work if I had numbers and not string..

 alex_age = type_age(f);
 alex_size = type_size(f);

 plot(alex_age,alex_size,'.r',sophie_age,sophie_size,'.b');

While above is just an example, I would like to be able to generate something like that to make a scatter plot of my variables differently coloured for each name. 尽管上面仅是一个示例,但我希望能够生成类似的内容,以便为每个名称绘制不同颜色的变量散点图。

So where I am stuck is to get the index (f) for the corresponding unique name within my array of string. 因此,我遇到的困难是获取字符串数组中对应的唯一名称的索引(f)。

On the other hand, if there is any easier way of doing that , please let me know. 另一方面,如果有更简便的方法,请告诉我。 I in fact have a huge data set. 我实际上有一个庞大的数据集。

Also I don't know if strcmp can be handy there - or using a switch case.. ?? 另外我也不知道strcmp是否可以派上用场-或使用开关盒。

Thanks a lot in advance! 在此先多谢!

It is already done for you. 已经为您完成。 Just use full syntax of the unique function: 只需使用unique功能的完整语法即可:

[un_a,b,m] = unique(A);

now 现在

un_a = 

'alex'
'david'
'sophie'


b =

10
 8
 9


m =

 1
 1
 3
 1
 2
 3
 2
 2
 3
 1

using m you can extract data from other related arrays like this: 使用m可以从其他相关数组中提取数据,如下所示:

>> alex_age = type_age( m==1 )
alex_age =

 1     2     4    10

>> david_age = type_age( m==2 )
david_age =

 5     7     8

>> sophie_age = type_age( m==3 )
sophie_age =

 3     6     9

None that the number in comparison operators is the index of the corresponding name in un_a array. 没有一个比较运算符中的数字是un_a数组中相应名称的索引。

EDIT @sophie, in your context un_a = A(b) , so b holds position of the unique elements in the original array A . 编辑 @sophie,在您的上下文中un_a = A(b) ,因此b保持原始数组A唯一元素的位置。 For example, you have four 'alex' (stored in un_a{1} ) in your array at positions 1,2,4,10, so you get b(1)=10 as index of one of them (last one, but I'm not sure this is documented, it may arise from the search algorithm). 例如,您在数组1,2,4,10的位置上有四个'alex'(存储在un_a{1} ),因此得到b(1)=10作为其中一个索引(最后一个,我不确定这是否已记录在案,它可能来自搜索算法。 If you want to get other indices, you do like this: 如果要获取其他索引,您可以这样:

alex_idx = find( m==m(b(1)) )
alex_idx =

 1
 2
 4
10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM