Something I would do is to first find the values of v
that are equal to a
which we will call ind
. Then, create a new output vector that has the output size equal to numel(v) + numel(ind)
, as we are replacing each value of a
that is in v
with an additional value, then use indexing to place our new values in.
Assuming that you have created a row vector v
, do the following:
%// Find all locations that are equal to a
ind = find(v == a);
%// Allocate output vector
out = zeros(1, numel(v) + numel(ind));
%// Determine locations in output vector that we need to
%// modify to place the value b in
indx = ind + (0:numel(ind)-1);
%// Determine locations in output vector that we need to
%// modify to place the value c in
indy = indx + 1;
%// Place values of b and c into the output
out(indx) = b;
out(indy) = c;
%// Get the rest of the values in v that are not equal to a
%// and place them in their corresponding spots.
rest = true(1,numel(out));
rest([indx,indy]) = false;
out(rest) = v(v ~= a);
The indx
and indy
statements are rather tricky, but certainly not hard to understand. For each index in v
that is equal to a
, what happens is that we need to shift the vector over by 1 for each index / location of v
that is equal to a
. The first value requires that we shift the vector over to the right by 1, then the next value requires that we shift to the right by 1 with respect to the previous shift, which means that we actually need to take the second index and shift by the right by 2 as this is with respect to the original index.
The next value requires that we shift to the right by 1 with respect to the second shift, or shifting to the right by 3 with respect to the original index and so on. These shifts define where we're going to place b
. To place c
, we simply take the indices generated for placing b
and move them over to the right by 1.
What's left is to populate the output vector with those values that are not equal to a
. We simply define a logical mask where the indices used to populate the output array have their locations set to false
while the rest are set to true
. We use this to index into the output and find those locations that are not equal to a
to complete the assignment.
Example:
v = [1,2,3,4,5,4,4,5];
a = 4;
b = 10;
c = 11;
Using the above code, we get:
out =
1 2 3 10 11 5 10 11 10 11 5
This successfully replaces every value that is 4 in v
with the tuple of [10,11]
.
v = [v(1) [5,5] v(3:end)]
would work though depending on how you want to use this there may be a better answer – Ockoa
in the vector, does each element get replaced with a 2 element vector? For example, ifv = [1,2,3,4,5]
anda = 4
andb = 10, c = 11
, does the new vector becomev = [1,2,3,10,11,5]
? – Weathersby