Populate a column vector with values from another column vector
2 views (last 30 days)
Show older comments
Raahim Muzaffar
on 5 Dec 2022
Commented: Walter Roberson
on 5 Dec 2022
Hey!
I have a column vector A that is 1x50 that contains values
I want to populate a 1x57 zero column vector B with the values of A at a certain index.
E.g. At index 0,1,2,5,7,8,11 should all be 0 - the other values of vector B should be populated from the non-zero values in vector A. How do i go about doing this?
Accepted Answer
Walter Roberson
on 5 Dec 2022
A = randi(9, 1, 50);
zeros_at = [0,1,2,5,7,8,11];
B = zeros(1, numel(A) + length(zeros_at));
mask = ismember((0 : length(B)-1), zeros_at);
B(~mask) = A;
B
2 Comments
Walter Roberson
on 5 Dec 2022
Marginally different approach:
A = randi(9, 1, 50);
zeros_at = [0,1,2,5,7,8,11];
B = zeros(1, numel(A) + length(zeros_at));
idx = setdiff(1:length(B), zeros_at+1);
B(idx) = A
More Answers (1)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!