Info

此问题已关闭。 请重新打开它进行编辑或回答。

how to get the same elements from many different vectors and each index for each vectors?

2 次查看(过去 30 天)
for example, now I have three vectors as below:
v1=['a','b','c','d','e','f']; v2=['a','c','d','f','g']; v3=['c','d','e','j'];
Now I want to get the same elements ['c','d'] and index1=[3,4],index2=[2,3],index3=[1,2];
Is there some good function for this?

回答(1 个)

KSSV
KSSV 2017-2-8
doc intersect
clc; clear all ;
v1=[{'a'},{'b'},{'c'},{'d'},{'e'},{'f'}];
v2=[{'a'},{'c'},{'d'},{'f'},{'g'}];
v3=[{'c'},{'d'},{'e'},{'j'}];
[a1,b1,c1] = intersect(v1,v2) ;
[a2,idx2,idx3] = intersect(a1,v3) ;
[common,idx1,c3] = intersect(v1,a2) ;
common
idx1
idx2
idx3
  4 个评论
Stephen23
Stephen23 2017-2-8
编辑:Stephen23 2017-2-8
@KSSV: why make this so complicated?:
v1=[{'a'},{'b'},{'c'},{'d'},{'e'},{'f'}];
why not simply write the cell array itself:
v1 = {'a','b','c','d','e','f'};
which is simpler to read and will make your code faster too.

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by