Replace vector value with two values โ€‹โ€‹in MATLAB

I need to create a function that takes a vector v as input and three scalars a , b and c . The function replaces each element v , which is equal to a with an array of two elements [b,c] .

For example, when using v = [1,2,3,4] and a = 2, b = 5, c = 5 output will look like this:

 out = [1,5,5,3,4] 

My first attempt was to try the following:

 v = [1,2,3,4]; v(2) = [5,5]; 

However, I get an error message, so I donโ€™t understand how to put two values โ€‹โ€‹instead of one in the vector, i.e. shift all the following values โ€‹โ€‹one position to the right so that the new two values โ€‹โ€‹fit into the vector and, therefore, the size of the vector will increase in one. Also, if there are several values โ€‹โ€‹of a in v , I'm not sure how to replace them immediately.

How to do it in MATLAB?

+5
source share
4 answers

Here's a solution using cell arrays:

 % remember the indices where a occurs ind = (v == a); % split array such that each element of a cell array contains one element v = mat2cell(v, 1, ones(1, numel(v))); % replace appropriate cells with two-element array v(ind) = {[bc]}; % concatenate v = cell2mat(v); 

Like Rayryeng's solution, it can replace several occurrences of a .

The problem mentioned by the silicon wafer that the array is resizing is solved here by interim storage of partial arrays in the cells of an array of cells. Converting back to an array concatenates these parts.

+6
source

Something I would like to do is first find the values โ€‹โ€‹of v that are equal to a , which we will call ind . Then create a new output vector whose output file size is numel(v) + numel(ind) , since we replace every value of a that is in v with an extra value, then use indexing to put our new values โ€‹โ€‹in.

Assuming you created a line vector v , follow these steps:

 %// 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 operators are quite complex, but certainly not hard to understand. For each index in v equal to a , what happens is that we need to shift the vector by 1 for each index / location v equal to a . The first value requires that we move the vector to the right by 1, then the next value requires that we shift to the right by 1 relative to the previous shift , which means that we really need to take the second pointer and shift right by 2, as this applies to source index.

The following value requires that we shift to the right by 1 relative to the second shift or shift to the right by 3 relative to the original index, and so on. These shifts determine where we are going to place b . To place c , we simply take the indices created to place b and move them to the right by 1.

It remains to fill the output vector with those values โ€‹โ€‹that are not equal to a . We simply define a logical mask in which the indices used to populate the output array have their locations set to false and the rest 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 code above, we get:

 out = 1 2 3 10 11 5 10 11 10 11 5 

This successfully replaces each value equal to 4 in v , with a tuple [10,11] .

+5
source

I think strrep deserves a mention here. Although it is called string replacement and warns you of entering non-w980>, it is still great for other numbers (including integers, doubles, and even complex numbers).

 v = [1,2,3,4] a = 2, b = 5, c = 5 out = strrep(v, a, [bc]) Warning: Inputs must be character arrays or cell arrays of strings. out = 1 5 5 3 4 
+4
source

You are not trying to overwrite an existing value in a vector. You are trying to resize a vector (i.e. the number of rows or columns in a vector) because you are adding an element. This will always lead to a redistribution of the vector in memory.

Create a new vector using the first and last half of v .

Say your index is stored in the variable index .

 index = 2; newValues = [5, 5]; x = [ v(1:index), newValues, v(index+1:end) ] x = 1 2 5 5 3 4 
+2
source

All Articles