Thread Subject: how to vectorize this

Subject: how to vectorize this

From: Xiao Chen

Date: 25 Dec, 2007 08:25:46

Message: 1 of 7

Hi
A very simple question:
I want to replace the diagonals of a matrix.
for example,
a=[1 1 1
   1 1 1
   1 1 1]
I need to change 1 in the diagonals to 2, and the result
a=[2 1 1
   1 2 1
   1 1 2]
I can write this code in a For loop.
I'm sure there is a much better way to do the same job,
hmmm, writing it in a vectorized form. But I have not been
able to figure one out.

Can somebody help me, thanks!!!

Subject: how to vectorize this

From: Jerome Briot

Date: 25 Dec, 2007 08:34:22

Message: 2 of 7

Hi,

try this :

a=ones(3);
a(1:size(a,1)+1:end)=2

or

a=diag([1 1 1])+1

Jérôme

Subject: how to vectorize this

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 25 Dec, 2007 09:59:58

Message: 3 of 7

In article <fkqeqa$ckp$1@fred.mathworks.com>,
xiao chen <yuanyuan5220985@gmail.com> wrote:
>I want to replace the diagonals of a matrix.

%the generalized solution for matrices that might not be square
numdiag = min([size(a,1),size(a,2)]);
diagidx = (0:numdiag-1)*size(a,1)+(1:numdiag));
a(diagidx) = a(diagidx) + 1;
--
  "I will speculate that [...] applications [...] could actually see a
  performance boost for most users by going dual-core [...] because it
  is running the adware and spyware that [...] are otherwise slowing
  down the single CPU that user has today" -- Herb Sutter

Subject: how to vectorize this

From: Bruno Luong

Date: 25 Dec, 2007 10:21:19

Message: 4 of 7

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message

> %the generalized solution for matrices that might not be
square
> numdiag = min([size(a,1),size(a,2)]);
> diagidx = (0:numdiag-1)*size(a,1)+(1:numdiag));
> a(diagidx) = a(diagidx) + 1;
> --

And if you prefer readability, diagidx can be obained by
sub2ind() as well:

numdiag = min(size(a));
diagidx = sub2ind(size(a),1:numdiag,1:numdiag);%<-row,column
a(diagidx) = a(diagidx) + 1;

Bruno

Subject: how to vectorize this

From: Huy

Date: 25 Dec, 2007 10:33:15

Message: 5 of 7

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fkqkau$97i$1@canopus.cc.umanitoba.ca>...
> In article <fkqeqa$ckp$1@fred.mathworks.com>,
> xiao chen <yuanyuan5220985@gmail.com> wrote:
> >I want to replace the diagonals of a matrix.
>
> %the generalized solution for matrices that might not be
square
> numdiag = min([size(a,1),size(a,2)]);
> diagidx = (0:numdiag-1)*size(a,1)+(1:numdiag));
> a(diagidx) = a(diagidx) + 1;
> --
> "I will speculate that [...] applications [...] could
actually see a
> performance boost for most users by going dual-core
[...] because it
> is running the adware and spyware that [...] are
otherwise slowing
> down the single CPU that user has today" --
Herb Sutter

pos = sub2ind(size(a),1:min(size(a)),1:min(size(a)));
a(pos) = 2;

Anh Huy Phan
RIKEN - BSI

Subject: how to vectorize this

From: us

Date: 26 Dec, 2007 01:21:02

Message: 6 of 7

"xiao chen"
<SNIP diag-filler evergreen...

one of the many other solutions

     m=zeros(4);
     m(eye(size(m))==1)=pi; % -or- a vec...
     disp(m);

us

Subject: how to vectorize this

From: Ching

Date: 26 Dec, 2007 16:01:09

Message: 7 of 7

assume M is a square matrix of size mXm, and d a vector of
size 1Xm, the following will replace the main diagonal with
the vector.
M=M-diag(diag(M))+diag(d)

In your example,
M=ones(3,3),
and d = 2*ones(1,3)

I don't know how slow it is due to the various diag
function calls.

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
evergreen us 30 Jan, 2008 20:38:19
code us 25 Dec, 2007 20:24:59
index us 25 Dec, 2007 20:24:59
eye us 25 Dec, 2007 20:24:59
diag us 25 Dec, 2007 20:24:59
logical indexing us 25 Dec, 2007 20:24:59
vectorize xiao chen 25 Dec, 2007 03:30:14
rssFeed for this Thread

Contact us at files@mathworks.com