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:
change diagonal to 1

Subject: change diagonal to 1

From: Wendy

Date: 27 Jun, 2010 18:13:05

Message: 1 of 2

Hi all,

I have a big square matrix, and I want to change the diagonal elements to 1. Is there a way that I can change the diagonal elements at once, instead of looping through all the elements one by one.

As an example,

A = [3 2 1;
4 5 6;
7 8 9];

and I want
A = [1 2 1;
4 1 6;
7 8 1].

Thanks,
Wendy

Subject: change diagonal to 1

From: James Tursa

Date: 27 Jun, 2010 18:31:05

Message: 2 of 2

"Wendy " <wlq121@gmail.com> wrote in message <i084bh$b3k$1@fred.mathworks.com>...
> Hi all,
>
> I have a big square matrix, and I want to change the diagonal elements to 1. Is there a way that I can change the diagonal elements at once, instead of looping through all the elements one by one.
>
> As an example,
>
> A = [3 2 1;
> 4 5 6;
> 7 8 9];
>
> and I want
> A = [1 2 1;
> 4 1 6;
> 7 8 1].
>
> Thanks,
> Wendy

[m n] = size(A);
A(1:m+1:end) = 1;

James Tursa

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