1

I came across this question from my book. this is the question:

write a function interleave which takes two vectors of the same length and returns a vector which is the result of interleaving the elements of two input vectors. for example,
interleave([1 2 3],[10 11 12]) = [1 10 2 11 3 12]
interleave([1 2 3 4], [-1 -2 -3 -4]) = [1 -1 2 -2 3 -3 4 -4]

in the book it says it's worth 9 marks. this is what i've done:

function res = interleave(A,B)
trans = [A' B'];
reshape_col = length(A)+length(B);
res = reshape(trans', 1, reshape_col)

what i've done gets me the right answer but it seems too less work for 9 marks. i'm not too sure if there is another way of getting to answer. any help will be appreciated. thanks

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113

1 Answers1

3

Concatenate and transpose is the way to go:

A = [1 2 3]
B = [10 11 12] 

temp = [A(:),B(:)].' %'%//'concatenate and transpose
out = temp(:)          $// reshape to vector

So your solution was quite close, it's just less generic. A(:) and B(:) ensures, that your result is independent from the orientation of the input vectors. Furthermore you should use the transpose operator .' and not ctranspose '. Using reshape is a little overkill, the colon : in temp(:) is sufficient, but if you want you can consider the empty input [] option of reshape, so you don't need to determine the total length before:

out = reshape([A(:),B(:)].',1,[]) %'
%// same as
temp = [A(:),B(:)].'              %'
out = temp(:).'

or

out = reshape([A(:),B(:)].',[],1) %'
%// same as 
temp = [A(:),B(:)].'              %'
out = temp(:)
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • Maybe a final `reshape` (instead of `(:)`) to return either a row or a column depending on the input shape? – Luis Mendo Jun 26 '15 at 12:35
  • thanks for the help. i checked out the link you attached and i just want to clarify something, does transpose operator .' somewhat works along the basis of doing element wise multiplication using .* when the dimensions don't agree? thanks – paurush_008 Jun 26 '15 at 12:44
  • @LuisMendo i like your idea of using `reshape` at the end. your suggestion and @thewaywewalk's suggestion works completely fine. i guess, at the end, it will just come down to the final preference. – paurush_008 Jun 26 '15 at 13:03
  • @paurush_008 `.'` is just transposition – Luis Mendo Jun 26 '15 at 13:18
  • 1
    @paurush_008 no there is no multiplication done. The difference becomes apparent when you have [**complex numbers**](http://stackoverflow.com/questions/25150027/using-transpose-versus-ctranspose-in-matlab/25150319#25150319). – Robert Seifert Jun 26 '15 at 13:18
  • alrighty. now i understand why it is better to use `.'` rather than just `'`. @thewaywewalk i very much like the idea of using empty parentheses as this is the way i've normally been taught. thanks a lot to both of you for the help – paurush_008 Jun 26 '15 at 23:22
  • @thewaywewalk - Guten Tag! I've created a MATLAB chat room for us to discuss things MATLAB related, or for discussions that span beyond the limitations of a single comment. Visit us when you have time! - http://chat.stackoverflow.com/rooms/81987/matlab – rayryeng Jun 30 '15 at 15:08
  • @rayryreng I actually try to spend less time on SO. A chat room does not help ;) But thanks for the invitation! – Robert Seifert Jun 30 '15 at 15:29