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:
merging/grouping arrays

Subject: merging/grouping arrays

From: Ondrej

Date: 3 Feb, 2009 23:30:19

Message: 1 of 6

Hi Guys,
 
I’m new with matlab and I have the following problem, it sounds simple but I’m struggling here with it,,…

I have 3 arrays A, B, C with the same number of elements (76x18) and I need to group the elements with the same indices in the matrix X as follows:
 
X = A(1,1) B(1,1) C(1,1)
      A(1,2) B(1,2) C(1,2)
      A(1,3) B(1,3) C(1,3)
        …………………
        …………………
      A(2,1) B(2,1) C(2,1)
        …………………
      A76,18) B(76,18) C(76,18)

I’ve tried the following but it doesn’t work properly,…

for i=1:1:76
    for j=1:1:18
        for n=1:1:1368
            
        row(n,:)= [x(i,j), y(i,j), pf_data(i,j)]
                
        end
    end
end


any help would be appreciated,…
thanks in advance, Ondrej

Subject: merging/grouping arrays

From: Roger Stafford

Date: 4 Feb, 2009 01:07:02

Message: 2 of 6

"Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gmak2b$o8l$1@fred.mathworks.com>...
> ......
> I have 3 arrays A, B, C with the same number of elements (76x18) and I need to group the elements with the same indices in the matrix X as follows:
>
> X = A(1,1) B(1,1) C(1,1)
> A(1,2) B(1,2) C(1,2)
> A(1,3) B(1,3) C(1,3)
> …………………
> …………………
> A(2,1) B(2,1) C(2,1)
> …………………
> A76,18) B(76,18) C(76,18)
> ......

 X = [reshape(A.',[],1),reshape(B.',[],1),reshape(C.',[],1)];

Roger Stafford

Subject: merging/grouping arrays

From: Ondrej

Date: 4 Feb, 2009 03:04:01

Message: 3 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gmapnm$e83$1@fred.mathworks.com>...
> "Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gmak2b$o8l$1@fred.mathworks.com>...
> > ......
> > I have 3 arrays A, B, C with the same number of elements (76x18) and I need to group the elements with the same indices in the matrix X as follows:
> >
> > X = A(1,1) B(1,1) C(1,1)
> > A(1,2) B(1,2) C(1,2)
> > A(1,3) B(1,3) C(1,3)
> > …………………
> > …………………
> > A(2,1) B(2,1) C(2,1)
> > …………………
> > A76,18) B(76,18) C(76,18)
> > ......
>
> X = [reshape(A.',[],1),reshape(B.',[],1),reshape(C.',[],1)];
>
> Roger Stafford

many thanks, works great!
Ondrej

Subject: merging/grouping arrays

From: Matt Fig

Date: 4 Feb, 2009 03:55:05

Message: 4 of 6

Just in case you are interested in seeing a working double for loop version:

[r,c] = size(A);
for ii = r:-1:1
    for jj = c:-1:1
        G((ii-1)*c + jj,:) = [A(ii,jj),B(ii,jj),C(ii,jj)];
    end
end




cd4Ua\.aUYcscWisss"`]szAV[mUjUUhbs\iVbhYsdU\z_9YY]ccWUMs`ch

Subject: merging/grouping arrays

From: Matt Fig

Date: 4 Feb, 2009 04:20:04

Message: 5 of 6

Actually, we can get away with one reshape call, which is a lot faster.


X = reshape([A.',B.',C.'],numel(A),3);





\_v`Xvpv`XZ_efPflkmccevDY^_%XvXvgkXY}d1f7gZlf}fd\vXfkv<\Xb\

Subject: merging/grouping arrays

From: Ondrej

Date: 4 Feb, 2009 04:20:04

Message: 6 of 6

"Matt Fig" <spamanon@yahoo.com> wrote in message <gmb3ip$bm8$1@fred.mathworks.com>...
> Just in case you are interested in seeing a working double for loop version:
>
> [r,c] = size(A);
> for ii = r:-1:1
> for jj = c:-1:1
> G((ii-1)*c + jj,:) = [A(ii,jj),B(ii,jj),C(ii,jj)];
> end
> end


many thanks, it is indeed very useful to me,...
cheers, ondrej

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