Thread Subject: Reciprocal of a column?

Subject: Reciprocal of a column?

From: Christian

Date: 25 Nov, 2009 11:46:05

Message: 1 of 4

Hello,
I am still very new to Matlab and I'm having a little problem.
I have a large Matrix and I want to add a column that is the reciprocal of another column I already have.

For example, I create a Matrix A: [3; 2; 1]
Then I want to create a second column which has the reciprocal of those values.

I tried:

A(:,2) = 1/A(:,1)

but that results in a seceond column of [0.33333 , 0 , 0]', so obviously something went wrong here.
The line A(1:3,2) = 1/A(1:3,2) gives the same result.

So I guess my question really is, what syntax do I have to use so that the correct value gets calculated in every row, because obviously the way I tried it doesn't work.

Sorry if this question seems pretty dumb, I couldnt really find the answer in the Matlab help and it would be really nice if you could clear this up for me or point me in the right direction.

Thanks,
Christian

Subject: Reciprocal of a column?

From: dpb

Date: 25 Nov, 2009 11:58:28

Message: 2 of 4

Christian wrote:
...
> For example, I create a Matrix A: [3; 2; 1]
> Then I want to create a second column which has the reciprocal of those values.
>
> I tried:
>
> A(:,2) = 1/A(:,1)
>
...

 >> a=[a, 1./a(:,1)]
a =
     3.0000 0.3333
     2.0000 0.5000
     1.0000 1.0000

doc horzcat

--

Subject: Reciprocal of a column?

From: Wayne King

Date: 25 Nov, 2009 11:59:03

Message: 3 of 4

"Christian " <vlad.impotech@gmx.net> wrote in message <hej5dt$ela$1@fred.mathworks.com>...
> Hello,
> I am still very new to Matlab and I'm having a little problem.
> I have a large Matrix and I want to add a column that is the reciprocal of another column I already have.
>
> For example, I create a Matrix A: [3; 2; 1]
> Then I want to create a second column which has the reciprocal of those values.
>
> I tried:
>
> A(:,2) = 1/A(:,1)
>
> but that results in a seceond column of [0.33333 , 0 , 0]', so obviously something went wrong here.
> The line A(1:3,2) = 1/A(1:3,2) gives the same result.
>
> So I guess my question really is, what syntax do I have to use so that the correct value gets calculated in every row, because obviously the way I tried it doesn't work.
>
> Sorry if this question seems pretty dumb, I couldnt really find the answer in the Matlab help and it would be really nice if you could clear this up for me or point me in the right direction.
>
> Thanks,
> Christian

Hi Christian, you should read about Matlab's . operator, is this context, you can see

>>help rdivide

A= [3; 2; 1];
A(:,2) = 1./A(:,1);

Hope that helps,
Wayne

Subject: Reciprocal of a column?

From: Christian

Date: 25 Nov, 2009 12:09:10

Message: 4 of 4

Thank you guys for the super-quick answers.
I can absolutely see my problem now. I just didnt know about that operator and how to use it, but now it's all clear :)

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com