I have 2 vectors v=[1 2 3 4 5 6 1 2 3] et k=[4 5 6 7 8 9].
I need to find the common part of the vectors which is [4 5 6] and replace it with a vector temp= [10 11 12].
The idea is to obtain at the end the vector cat= [1 2 3 10 11 12 1 2 3 7 8 9].
Please note that the vectors v and k don't have the same size.
Can anyone advise me with a method ?
Thanks in advance

1 Kommentar

Stephen23
Stephen23 am 20 Jul. 2017
Bearbeitet: Stephen23 am 20 Jul. 2017
" find the common part of the vectors"
Is there always one common subvector? How long can it be? Is there always a common subvector? Do you want all common subvectors, or just the longest one? Does the replacement vector always have the same length as the original subvector?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 20 Jul. 2017
Bearbeitet: Jan am 20 Jul. 2017

1 Stimme

v = [1 2 3 4 5 6 1 2 3]
k = [4 5 6 7 8 9]
c = [4 5 6]
in_v = strfind(v, c); % Replace c in v
if ~isempty(in_v)
for ic = 1:length(c)
v(in_v + ic -1) = c(ic);
end
end
in_k = strfind(k, c); % Remove c from k
if ~isempty(in_k)
match = true(1, length(k));
for ic = 1:length(c)
match(in_k + ic -1) = false;
end
k = k(match);
end
result = [v, k]
Does this do what you want? Then it can be improved to run faster on demand.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 20 Jul. 2017

0 Stimmen

t1 = ismember(k,v);
v(ismember(v,k)) = temp;
out = [v,k(~t1)];

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Jul. 2017

Bearbeitet:

am 20 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by