필터 지우기
필터 지우기

How can I test that tab values exist in other sequence?

조회 수: 3 (최근 30일)
Mira le
Mira le 2023년 6월 13일
답변: Parag Jhunjhunwala 2023년 6월 13일
Hello every one
I have a problem
I want you help me
I have a vect = [ 5 34] for example
and an other sequnce that contains many values:
seq=[1 2 3 4 5 12 15 20 23 25 30 34 35 40]
how can I obtain the position of each value of vect if exist in seq
here in this exampe
the positions are:5 and 12
please hepl me!

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 6월 13일
편집: Dyuman Joshi 2023년 6월 13일
vect = [5 34];
seq=[1 2 3 4 5 12 15 20 23 25 30 34 35 40];
[~,ind] = ismember(vect, seq)
ind = 1×2
5 12
Note that this only returns the first occurence of an element in seq.
%That is, if seq has multiples of an elements, it will return the first index
seq0=[1 2 3 4 5 12 15 20 23 25 30 34 35 40 5];
%15th element of seq0 is 5, so [5 12 15] elements of seq0 are common with vect
%but it only returns 5, the first index at which 5 is found in seq0
[~,ind] = ismember(vect, seq0)
ind = 1×2
5 12
If an element is not present in the 2nd array, it will return 0 for it.

추가 답변 (2개)

Ronit
Ronit 2023년 6월 13일
vect = [5 34 42];
seq = [1 2 3 4 5 12 15 20 23 25 30 34 35 40];
[~, loc] = ismember(vect, seq);
disp(loc);
5 12 0
This will give the indices of that vect element in seq and 0 if it's not present.

Parag Jhunjhunwala
Parag Jhunjhunwala 2023년 6월 13일
There are two ways to find the position of each value of vect in seq:
1. Using find() function:
pos=zeros(1,length(vect));
for i=1:length(vect)
if(~isempty(find(seq==vect(i), 1)))
pos(i)=find(seq==vect(i));
end
end
2. Using ismember() inbuilt function:
[~,pos]=ismember(vect,seq);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by