Got Questions? Get Answers.
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:
How can I merge 2 arrays?

Subject: How can I merge 2 arrays?

From: James Ramm

Date: 28 Sep, 2010 08:53:05

Message: 1 of 4

Hi
I have 2 MxN arrays of frequency and voltage data.
I would like to merge them to give a M-by-2N array of alternating freq and voltage columns, i.e:

Freq1 Voltage1 Freq2 Voltage2 Freq3 Voltage3........FreqN VoltageN


I also need to be able to do it in a dynamic routine - one that can work for any M-by-N array pairs.
I have no idea how to do this - can anyone help?

Subject: How can I merge 2 arrays?

From: Roger Stafford

Date: 28 Sep, 2010 09:36:04

Message: 2 of 4

"James Ramm" <james@whitehocott.plus.com> wrote in message <i7sadh$6nf$1@fred.mathworks.com>...
> Hi
> I have 2 MxN arrays of frequency and voltage data.
> I would like to merge them to give a M-by-2N array of alternating freq and voltage columns, i.e:
>
> Freq1 Voltage1 Freq2 Voltage2 Freq3 Voltage3........FreqN VoltageN
>
>
> I also need to be able to do it in a dynamic routine - one that can work for any M-by-N array pairs.
> I have no idea how to do this - can anyone help?
- - - - - - - - -
  Let F and V be m x n arrays.

 FV = reshape([F;V],m,[]); % Alternating F and V columns

Roger Stafford

Subject: How can I merge 2 arrays?

From: Terry

Date: 28 Sep, 2010 10:24:04

Message: 3 of 4

"James Ramm" <james@whitehocott.plus.com> wrote in message <i7sadh$6nf$1@fred.mathworks.com>...
> Hi
> I have 2 MxN arrays of frequency and voltage data.
> I would like to merge them to give a M-by-2N array of alternating freq and voltage columns, i.e:
>
> Freq1 Voltage1 Freq2 Voltage2 Freq3 Voltage3........FreqN VoltageN
>
>
> I also need to be able to do it in a dynamic routine - one that can work for any M-by-N array pairs.
> I have no idea how to do this - can anyone help?

fCols = 1:2:2*N-1; % aka odds
vCols = 2:2:2*N; % aka evens
fv = zeros(M,2*N);
fv(:,fCols) = f;
fv(:,vCols) = v;

...which you might consolidate and add an assertion to to get this:
[M N] = size(f);
assert(all([M N]==size(v)));
fv = zeros(M,2*N);
fv(:,1:2:2*N-1) = f;
fv(:,2:2:2*N) = v;

The larger M and/or N are, the more likely this method is to be faster than one involving reshaping, I believe.

Terry

Subject: How can I merge 2 arrays?

From: Jos (10584)

Date: 28 Sep, 2010 11:14:05

Message: 4 of 4

"Terry " <xterry.xnycum@xgmail.no-xs.com> wrote in message <i7sfo4$l7b$1@fred.mathworks.com>...
> "James Ramm" <james@whitehocott.plus.com> wrote in message <i7sadh$6nf$1@fred.mathworks.com>...
> > Hi
> > I have 2 MxN arrays of frequency and voltage data.
> > I would like to merge them to give a M-by-2N array of alternating freq and voltage columns, i.e:
> >
> > Freq1 Voltage1 Freq2 Voltage2 Freq3 Voltage3........FreqN VoltageN
> >
> >
> > I also need to be able to do it in a dynamic routine - one that can work for any M-by-N array pairs.
> > I have no idea how to do this - can anyone help?
>
> fCols = 1:2:2*N-1; % aka odds
> vCols = 2:2:2*N; % aka evens
> fv = zeros(M,2*N);
> fv(:,fCols) = f;
> fv(:,vCols) = v;
>
> ...which you might consolidate and add an assertion to to get this:
> [M N] = size(f);
> assert(all([M N]==size(v)));
> fv = zeros(M,2*N);
> fv(:,1:2:2*N-1) = f;
> fv(:,2:2:2*N) = v;
>
> The larger M and/or N are, the more likely this method is to be faster than one involving reshaping, I believe.
>
> Terry

A more concise approach along the same lines:

% some test data
  f = repmat(1:5,3,1)
  v = 10* f
% engine
  ix = 2*(1:size(f,2))
  fv(:,[ix-1 ix]) = [f v] % build final matrix in one step

hth
Jos

Tags for this Thread

No tags are associated with 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