I would like to add the elements to the array using their index.
array_in = [1 5 6 8 9];
index = [2 4];
newElements = [25 67];
index = index + (0:length(index)-1);
expected output:
array_out = [1 25 5 6 67 8 9];
1.using for loop:
tmp = array_in;
for idx = 1:length(index)
array_out = cat(2,tmp(1:index(idx)-1),newElements(idx),tmp(index(idx):end));
tmp = array_out;
end
2.The code for Calling a Function Using Its Handle:
fcn_Insert = @(a, x, n) cat(2, x(1:n-1), a, x(n:end));
array_out = fcn_Insert(newElements,array_in,index)
The above code.2 (function) is not working? can any one suggest the solution. Are there any other better solutions?