필터 지우기
필터 지우기

Make two vectors equal with zero inserted in the missed location values of the small vector

조회 수: 4 (최근 30일)
I have two vectors one small and the another is larger. I want to check element by element and if there is an element in first vector does not equal to the corresponding element in the second vector, put zero at this missed location in small vector, so as finally the small vector will have the same size of the first vector, but with zero padded in the missing element positions.
for example
V1=[1,2,3,4,1,2,3,4];
V2=[1,2,4,1,3,4];
How to get V2 with this [1,2,0,4,1,0,3,4]

채택된 답변

Matt J
Matt J 2023년 5월 14일
편집: Matt J 2023년 5월 14일
V1=[1,2,3,4,1,2,3,4];
V2=[1,2,4,1,3,4];
n1=length(V1);
for i=1:n1
if i>length(V2)
V2(end+1:n1)=0; break
elseif V1(i)~=V2(i)
V2=[V2(1:i-1),0,V2(i:end)];
V2=V2(1:min(end,n1));
end
end
V1
V1 = 1×8
1 2 3 4 1 2 3 4
V2
V2 = 1×8
1 2 0 4 1 0 3 4

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 14일
for loop. No point trying to be smart about it, if you can guarantee that the second one is always a series of sub-sequences of the first.
If you were trying to find the "best" match between two sequences that have differences in places, then that would fall into the category of "sequence alignment", and there would be multiple algorithms for that; some of them are mentioned at https://www.mathworks.com/help/bioinfo/sequence-alignment.html

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by