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:
Beginner_help_request

Subject: Beginner_help_request

From: joey hart

Date: 30 Sep, 2010 06:40:11

Message: 1 of 13

hi,

I am a new user to Matlab and need some help please.

I have two matrices

A = [ 1
         2
         3]

B = [ 10
        20
        30
        40
        50
        60]

I would like to do the addition of elements in A and B in such a way as:

[ 1+ 10
  1+ 20
  2+ 30
  2 + 40
  3 + 50
  3 + 60 ]

I hope for a generalized solution since A and B can increase columnwise, so for example , I would do an example where elements increase:

A = [ 15
        12
        33
        46]

B =[ 10
       20
       30
       40
       50
       60
       70
       80
       90
      100
      110
      120]

I would like to addition as:

[ 15 + 10
   15 + 20
   15 + 30
   12 + 40
   12 + 50
   12 + 60
   33 + 70
   33 + 80
   33 + 90
  46 + 100
  46 + 110
  46 + 120]

So to generalize how the size increase pattern of A and B works, have a look at this table:

A B
3x1 6x1
4x1 12x1
5x1 20x1
6x1 30x1
. .
. .
. .

So basically , if A and B get larger they are done so by increasing factors of 2, 3 ,4 ,5,...... as seen in the above table.

Hopefully, I made my problem clear.

Please help me folks.

Truly,
joey
   

Subject: Beginner_help_request

From: Grzegorz Knor

Date: 30 Sep, 2010 06:54:03

Message: 2 of 13

"joey hart" <joeyz0123@gmail.com> wrote in message <i81bca$4hp$1@fred.mathworks.com>...
> hi,
>
> I am a new user to Matlab and need some help please.
>
> I have two matrices
>
> A = [ 1
> 2
> 3]
>
> B = [ 10
> 20
> 30
> 40
> 50
> 60]
>
> I would like to do the addition of elements in A and B in such a way as:
>
> [ 1+ 10
> 1+ 20
> 2+ 30
> 2 + 40
> 3 + 50
> 3 + 60 ]
>
> I hope for a generalized solution since A and B can increase columnwise, so for example , I would do an example where elements increase:
>
> A = [ 15
> 12
> 33
> 46]
>
> B =[ 10
> 20
> 30
> 40
> 50
> 60
> 70
> 80
> 90
> 100
> 110
> 120]
>
> I would like to addition as:
>
> [ 15 + 10
> 15 + 20
> 15 + 30
> 12 + 40
> 12 + 50
> 12 + 60
> 33 + 70
> 33 + 80
> 33 + 90
> 46 + 100
> 46 + 110
> 46 + 120]
>
> So to generalize how the size increase pattern of A and B works, have a look at this table:
>
> A B
> 3x1 6x1
> 4x1 12x1
> 5x1 20x1
> 6x1 30x1
> . .
> . .
> . .
>
> So basically , if A and B get larger they are done so by increasing factors of 2, 3 ,4 ,5,...... as seen in the above table.
>
> Hopefully, I made my problem clear.
>
> Please help me folks.
>
> Truly,
> joey
>

DOC REPMAT
DOC LENGTH

    r = length(B)/length(A);
    tmp = repmat(A',r,1);
    tmp = tmp(:);
    answer = B + tmp;

regards
Grzegorz

Subject: Beginner_help_request

From: Matt J

Date: 30 Sep, 2010 07:07:04

Message: 3 of 13

"Grzegorz Knor" <gknor@o2.pl> wrote in message <i81c6b$oe6$1@fred.mathworks.com>...

> DOC REPMAT
> DOC LENGTH
>
> r = length(B)/length(A);
> tmp = repmat(A',r,1);
> tmp = tmp(:);
> answer = B + tmp;
=========

No need for repmat,

    r = length(B)/length(A);
    B = bsxfun(@plus, A', reshape(B,r,[]) );
    B= B(:);

Subject: Beginner_help_request

From: Jos (10584)

Date: 30 Sep, 2010 08:14:07

Message: 4 of 13

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i81cuo$ned$1@fred.mathworks.com>...
> "Grzegorz Knor" <gknor@o2.pl> wrote in message <i81c6b$oe6$1@fred.mathworks.com>...
>
> > DOC REPMAT
> > DOC LENGTH
> >
> > r = length(B)/length(A);
> > tmp = repmat(A',r,1);
> > tmp = tmp(:);
> > answer = B + tmp;
> =========
>
> No need for repmat,
>
> r = length(B)/length(A);
> B = bsxfun(@plus, A', reshape(B,r,[]) );
> B= B(:);

The first two lines can be condensed:
C = bsxfun(@plus, A', reshape(B, [], numel(A) ) )

Here is a fast approach using indexing:
C = A(ceil((1:numel(B))/3)) + B

hth
Jos

Subject: Beginner_help_request

From: joey hart

Date: 30 Sep, 2010 08:27:22

Message: 5 of 13

thank you.

joe

Subject: Beginner_help_request

From: joey hart

Date: 30 Sep, 2010 23:50:23

Message: 6 of 13

Folks,

If i have 2 vectors as follows:

A = [ 2
        4
        6
        8
        10
        12
        14
        16]

and

B = [ 100
         200
         300
         400
         500
         600]

I'd like to create a matrix as follows:

A_B = [ 2 100
              2 200
              2 300
              4 100
              4 200
              4 300
              6 100
              6 200
              6 300
              8 100
              8 200
              8 300
              10 400
              10 500
              10 600
              12 400
              12 500
              12 600
              14 400
              14 500
              14 600
              16 400
              16 500
              16 600]

So basically the first 4 number elements in A correspond to the first 3 elements in B, and the last 4 elements in A correspond to the last 3 elements in B.

Subject: help please

From: joey hart

Date: 1 Oct, 2010 01:03:05

Message: 7 of 13


Hi guys,

pleading for some urgent help with respect to the last post.

joe

Subject: help please

From: ImageAnalyst

Date: 1 Oct, 2010 02:11:44

Message: 8 of 13

Uh, I think you just solved it. That looks like a perfectly good
solution to me. I don't see anything wrong with it. Do you have a
problem with it?

Do you have a question (such as you need it to work with arbitrarily
sized matrices)? You didn't ask a question of us yet.

Subject: help please

From: joey hart

Date: 1 Oct, 2010 04:43:04

Message: 9 of 13

no, there is no arbitrary sized matrix I have to work with, just the ones given only:

A = [ 2
        4
        6
        8
        10
        12
        14
        16]

and

B = [ 100
         200
         300
         400
         500
         600]

Can anyone help me out?

joey

Subject: help please

From: joey hart

Date: 1 Oct, 2010 06:32:08

Message: 10 of 13


hello,

my question is how can I combine matrices and B to form the single matrix A_B?

A = [ 2
        4
        6
        8
        10
        12
        14
        16]

and

B = [ 100
         200
         300
         400
         500
         600]

I'd like to create a matrix as follows:

A_B = [ 2 100
              2 200
              2 300
              4 100
              4 200
              4 300
              6 100
              6 200
              6 300
              8 100
              8 200
              8 300
              10 400
              10 500
              10 600
              12 400
              12 500
              12 600
              14 400
              14 500
              14 600
              16 400
              16 500
              16 600]

Subject: help please

From: Grzegorz Knor

Date: 1 Oct, 2010 07:18:05

Message: 11 of 13

"joey hart" <joeyz0123@gmail.com> wrote in message <i83v98$sef$1@fred.mathworks.com>...
>
> hello,
>
> my question is how can I combine matrices and B to form the single matrix A_B?
>
> A = [ 2
> 4
> 6
> 8
> 10
> 12
> 14
> 16]
>
> and
>
> B = [ 100
> 200
> 300
> 400
> 500
> 600]
>
> I'd like to create a matrix as follows:
>
> A_B = [ 2 100
> 2 200
> 2 300
> 4 100
> 4 200
> 4 300
> 6 100
> 6 200
> 6 300
> 8 100
> 8 200
> 8 300
> 10 400
> 10 500
> 10 600
> 12 400
> 12 500
> 12 600
> 14 400
> 14 500
> 14 600
> 16 400
> 16 500
> 16 600]

I do not know if this is the quickest solution:

a = repmat(A',3,1);
b = repmat(reshape(B,3,2),4,1);
a_b = [a(:) b(:)];

Grzegorz

Subject: help please

From: ImageAnalyst

Date: 1 Oct, 2010 09:58:27

Message: 12 of 13

Well since you said "no, there is no arbitrary sized matrix I have to
work with, just the ones given only" let me be much more explicit
then. Perhaps you don't know how to create rows or continue lines.
To put data in a new row, insert a semicolon. To continue to the next
line, add three dots. Here is how you create the matrix:

A_B = [ 2 100;...
              2 200;...
              2 300;...
              4 100;...
              4 200;...
              4 300;...
              6 100;...
              6 200;...
              6 300;...
              8 100;...
              8 200;...
              8 300;...
              10 400;...
              10 500;...
              10 600;...
              12 400;...
              12 500;...
              12 600;...
              14 400;...
              14 500;...
              14 600;...
              16 400;...
              16 500;...
              16 600]
This will create and display exactly what you wrote.

Subject: help please

From: dpb

Date: 1 Oct, 2010 13:36:41

Message: 13 of 13

joey hart wrote:
> no, there is no arbitrary sized matrix I have to work with, just the
> ones given only:
>
> A = [ 2
> 4
> 6
> 8
> 10
> 12
> 14
> 16]
>
> and
>
> B = [ 100
> 200
> 300
> 400
> 500
> 600]
>
> Can anyone help me out?

doc repmat % and a loop come to mind...

--

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