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:
matrix manipulation

Subject: matrix manipulation

From: antonio ferreira

Date: 22 Nov, 2010 19:38:03

Message: 1 of 4

Hi all.

I have a simple matrix A=[2 3;4 6; 7 8] and I want to divide only the second column by 2, i.e., the result must be b=[2 1.5; 4 3;7 4].If I want to divide the second column by 2 I must do A(:,2)/2 but how can I "index" the first column?Thanks a lot for the suggestions.

Regards

Subject: matrix manipulation

From: someone

Date: 22 Nov, 2010 19:56:03

Message: 2 of 4

"antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <icegqr$d6v$1@fred.mathworks.com>...
> Hi all.
>
> I have a simple matrix A=[2 3;4 6; 7 8] and I want to divide only the second column by 2, i.e., the result must be b=[2 1.5; 4 3;7 4].If I want to divide the second column by 2 I must do A(:,2)/2 but how can I "index" the first column?Thanks a lot for the suggestions.
>
> Regards

% No need to 'index" the first column, try:

A=[2 3;4 6; 7 8];
b = A;
b(:,2) = A(:,2)/2

b =

    2.0000 1.5000
    4.0000 3.0000
    7.0000 4.0000

>>

Subject: matrix manipulation

From: Sean de

Date: 22 Nov, 2010 20:05:05

Message: 3 of 4

"someone" <someone@somewhere.net> wrote in message <icehsj$mvk$1@fred.mathworks.com>...
> "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <icegqr$d6v$1@fred.mathworks.com>...
> > Hi all.
> >
> > I have a simple matrix A=[2 3;4 6; 7 8] and I want to divide only the second column by 2, i.e., the result must be b=[2 1.5; 4 3;7 4].If I want to divide the second column by 2 I must do A(:,2)/2 but how can I "index" the first column?Thanks a lot for the suggestions.
> >
> > Regards
>
> % No need to 'index" the first column, try:
>
> A=[2 3;4 6; 7 8];
> b = A;
> b(:,2) = A(:,2)/2
>
> b =
>
> 2.0000 1.5000
> 4.0000 3.0000
> 7.0000 4.0000
>
> >>

For multiple columns/divisions etc. you could also use BSXFUN for this:

bsxfun(@rdivide,A,[1 2])

The advantage of this is if you wanted to divide the first and second column by something you wouldn't have to extract them each individually. E.g. divide the first column by 3 and the second by 2

bsxfun(@rdivide,A,[3 2])

Subject: matrix manipulation

From: antonio ferreira

Date: 23 Nov, 2010 10:02:07

Message: 4 of 4

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iceidh$rod$1@fred.mathworks.com>...
> "someone" <someone@somewhere.net> wrote in message <icehsj$mvk$1@fred.mathworks.com>...
> > "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <icegqr$d6v$1@fred.mathworks.com>...
> > > Hi all.
> > >
> > > I have a simple matrix A=[2 3;4 6; 7 8] and I want to divide only the second column by 2, i.e., the result must be b=[2 1.5; 4 3;7 4].If I want to divide the second column by 2 I must do A(:,2)/2 but how can I "index" the first column?Thanks a lot for the suggestions.
> > >
> > > Regards
> >
> > % No need to 'index" the first column, try:
> >
> > A=[2 3;4 6; 7 8];
> > b = A;
> > b(:,2) = A(:,2)/2
> >
> > b =
> >
> > 2.0000 1.5000
> > 4.0000 3.0000
> > 7.0000 4.0000
> >
> > >>
>
> For multiple columns/divisions etc. you could also use BSXFUN for this:
>
> bsxfun(@rdivide,A,[1 2])
>
> The advantage of this is if you wanted to divide the first and second column by something you wouldn't have to extract them each individually. E.g. divide the first column by 3 and the second by 2
>
> bsxfun(@rdivide,A,[3 2])

Ok.Many thanks for your suggestions.Best Regards

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