Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
vx2008
vx2008 2017 年 2 月 8 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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.
KSSV
KSSV 2017 年 2 月 8 日
@Stephen yes you are right....thank you for the comment.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by