Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Interleave every 2 values

Subject: Interleave every 2 values

From: Sylvie

Date: 19 Feb, 2009 01:42:01

Message: 1 of 3

Hello,

s=[linspace(0,3,22)];

I would like to interleave every 2 numbers:
[s(1),s(3),s(2),s(4)...s(22)]

Any suggestion

Sylvie

Subject: Interleave every 2 values

From: Jos

Date: 19 Feb, 2009 08:43:02

Message: 2 of 3

"Sylvie " <sylpel@yorku.ca> wrote in message <gnidd9$232$1@fred.mathworks.com>...
> Hello,
>
> s=[linspace(0,3,22)];
>
> I would like to interleave every 2 numbers:
> [s(1),s(3),s(2),s(4)...s(22)]
>
> Any suggestion
>
> Sylvie

So you want numbers to repeat, as in [s1, s3, s2, s4, s3, s5, s4, s6 ... s22]

s = 1:8
s2 = reshape([s(1:end-2) ; s(3:end)],1,[])

If this is not what you want, try again ...

hth
Jos

Subject: Interleave every 2 values

From: us

Date: 19 Feb, 2009 09:15:04

Message: 3 of 3

"Sylvie"
> s=[linspace(0,3,22)];
> I would like to interleave every 2 numbers:
> [s(1),s(3),s(2),s(4)...s(22)]...

one of the many possible solutions

     s=1:16; % <- must be divisible by 4
     r=[s(1:2:end);s(2:2:end)];
     r=mat2cell(r,[1,1],repmat(2,1,numel(s)/4));
     r=cat(2,r{:})
% note: NO repeats...
% r = 1 3 2 4 5 7 6 8 9 11 10 12 13 15 14 16

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us