interleaving arrays of different size
3 views (last 30 days)
Show older comments
So I made a script that can weave two arrays of the same size
x = [1 2 3];
output = zeros(1,2*length(x));
output(1:2:end) = x;
y = [4 5 6];
output(output ==0 ) = y
But what can I do if I want to weave two arrays of different size.
1 Comment
Image Analyst
on 28 Jun 2015
There are probably lots of algorithms you can use. I don't know which one you want to do. Why do you want to do this anyway? Maybe if we knew more about the need , then we could figure out how to do it. Otherwise, why don't you give an example, like interleaving a 3 element vector into a 10 element vector?
Accepted Answer
Guillaume
on 28 Jun 2015
Edited: Guillaume
on 29 Jun 2015
Note that for row vectors of equal length, the following is probably more efficient (and works with vectors which contain 0 unlike your algorithm, or NaN or anything):
output = [x; y];
output = output(:).';
As Image Analyst says, you don't say what interleaving vectors of different size mean. Assuming you want all the extra elements of the longer vector together at the end:
commonsize = min(numel(x), numel(y));
output = [x(1:commonsize); y(1:commonsize)];
output = [output(:).', x(commonsize+1:end), y(commonsize+1:end)]
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!