Reputation: 219
I have a problem to solve in either Matlab or R (preferably in R).
Imagine I have a vector A with 10 elements.
I have also a vector B with 30 elements, of which 10 have value 'x'.
Now, I want to replace all the 'x' in B by the corresponding values taken from A, in the order that is established in A. Once a value in A is taken, the next one is ready to be used when the next 'x' in B is found.
Note that the sizes of A and B are different, it's the number of 'x' cells that coincides with the size of A.
I have tried different ways to do it. Any suggestion on how to program this?
Upvotes: 0
Views: 1780
Reputation: 32930
In MATLAB it's quite simple, use logical indexing:
B(B == 'x') = A;
Upvotes: 2
Reputation: 42689
As long as the number of x
entries in B
matches the length of A
, this will do what you want:
B[B=='x'] <- A
(It should be clear that this is the R
solution.)
Upvotes: 2