Thread Subject: How to do vectorize this simple code?

Subject: How to do vectorize this simple code?

From: Henry

Date: 14 Nov, 2008 22:09:03

Message: 1 of 11

I want to vectorize this simple snippet:

for i=1:num_elements
    y(i)=my_matrix(row(i),col(i));
end

I tried y=my_matrix(row,col)
and y=my_matrix([row col])

but nothing seems to work. Any pointers? Thanks.

Subject: How to do vectorize this simple code?

From: Kenneth Eaton

Date: 14 Nov, 2008 22:15:07

Message: 2 of 11

"Henry " <henry@fullspectrumengineering.com> wrote in message <gfkstu$1id$1@fred.mathworks.com>...
> I want to vectorize this simple snippet:
>
> for i=1:num_elements
> y(i)=my_matrix(row(i),col(i));
> end
>
> I tried y=my_matrix(row,col)
> and y=my_matrix([row col])
>
> but nothing seems to work. Any pointers? Thanks.

Check out the SUB2IND function.

hth,
Ken

Subject: How to do vectorize this simple code?

From: Scott

Date: 14 Nov, 2008 22:18:01

Message: 3 of 11

If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).

y(i)=diag(my_matrix(row(i),col(i)));

Subject: How to do vectorize this simple code?

From: John D'Errico

Date: 14 Nov, 2008 22:35:03

Message: 4 of 11

"Scott " <crazyivan84__remove__@hotmail.com> wrote in message <gfktep$9al$1@fred.mathworks.com>...
> If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).
>
> y(i)=diag(my_matrix(row(i),col(i)));

This is an extremely inefficient way to do it of course.

John

Subject: How to do vectorize this simple code?

From: Mahdieh

Date: 14 Nov, 2008 23:17:03

Message: 5 of 11

"Henry " <henry@fullspectrumengineering.com> wrote in message <gfkstu$1id$1@fred.mathworks.com>...
> I want to vectorize this simple snippet:
>
> for i=1:num_elements
> y(i)=my_matrix(row(i),col(i));
> end
>
> I tried y=my_matrix(row,col)
> and y=my_matrix([row col])
>
> but nothing seems to work. Any pointers? Thanks.

Hi Henry

I guess this works:

U = mymatrix(row,:);
V = U(:,col);
y = diag(V);

-Mah

Subject: How to do vectorize this simple code?

From: Henry

Date: 14 Nov, 2008 23:36:02

Message: 6 of 11

Hmm all those suggestions don't seem to be very efficient compared to my original code of:
for i=1:num_elements
    y(i)=my_matrix(row(i),col(i));
end

I just thought I could write it as a single line like y=my_matrix(row,col) where row and col are vectors and it would match them up as pairs and return a vector.

Seems like a common procedure and I'm surprised matlab doesn't have it built in...

"Mahdieh" <mahdieh.emrani@capitalhealth.ca> wrote in message <gfl0tf$srs$1@fred.mathworks.com>...
> "Henry " <henry@fullspectrumengineering.com> wrote in message <gfkstu$1id$1@fred.mathworks.com>...
> > I want to vectorize this simple snippet:
> >
> > for i=1:num_elements
> > y(i)=my_matrix(row(i),col(i));
> > end
> >
> > I tried y=my_matrix(row,col)
> > and y=my_matrix([row col])
> >
> > but nothing seems to work. Any pointers? Thanks.
>
> Hi Henry
>
> I guess this works:
>
> U = mymatrix(row,:);
> V = U(:,col);
> y = diag(V);
>
> -Mah

Subject: How to do vectorize this simple code?

From: Scott

Date: 14 Nov, 2008 23:43:01

Message: 7 of 11

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gfkuen$m7v$1@fred.mathworks.com>...
> "Scott " <crazyivan84__remove__@hotmail.com> wrote in message <gfktep$9al$1@fred.mathworks.com>...
> > If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).
> >
> > y(i)=diag(my_matrix(row(i),col(i)));
>
> This is an extremely inefficient way to do it of course.
>
> John

Actually, I've always found sub2ind to be quite slow.
Consider:

rows=randint(5,100000,[1,10]);
cols=randint(5,100000,[1,10]);
testMat=magic(10);

%Sub2ind
tic
resultsInd=zeros(100000,5);
for i=1:100000
    index=sub2ind(size(testMat),rows(:,i),cols(:,i));
    resultsInd(i,:)=testMat(index);
end
toc

%Diag
tic
resultsDiag=zeros(100000,5);
for i=1:100000
    resultsDiag(i,:)=diag(testMat(rows(:,i),cols(:,i)));
end
toc

isequal(resultsInd,resultsDiag)


With the result:
Elapsed time is 6.405682 seconds. (sub2ind)
Elapsed time is 0.293780 seconds. (diag)

ans =

     1

Subject: How to do vectorize this simple code?

From: Husam Aldahiyat

Date: 15 Nov, 2008 03:24:02

Message: 8 of 11

Your comparison is invalid.

Subject: How to do vectorize this simple code?

From: Kenneth Eaton

Date: 15 Nov, 2008 03:58:01

Message: 9 of 11

"Henry " <henry@fullspectrumengineering.com> wrote in message <gfl212$m0t$1@fred.mathworks.com>...
> Hmm all those suggestions don't seem to be very efficient compared to my original code of:
> for i=1:num_elements
> y(i)=my_matrix(row(i),col(i));
> end
>
> I just thought I could write it as a single line like y=my_matrix(row,col) where row and col are vectors and it would match them up as pairs and return a vector.
>
> Seems like a common procedure and I'm surprised matlab doesn't have it built in...

I don't know if SUB2IND is vectorized. If not, try this:

N = size(my_matrix,1);
ind = N*(col-1)+row;
y = my_matrix(size(my_matrix,1)*(col-1)+row);

hth,
Ken

Subject: How to do vectorize this simple code?

From: Kenneth Eaton

Date: 15 Nov, 2008 04:20:19

Message: 10 of 11

"Kenneth Eaton" <Kenneth.dot.Eaton@cchmc.dot.org> wrote in message <gflhc9$h1d$1@fred.mathworks.com>...
>
> I don't know if SUB2IND is vectorized. If not, try this:
>
> N = size(my_matrix,1);
> ind = N*(col-1)+row;
> y = my_matrix(size(my_matrix,1)*(col-1)+row);

Oops, mistyped that a little; you can remove the first 2 lines, using the third as a one-liner, or make the third line: y = my_matrix(ind);

Ken

Subject: How to do vectorize this simple code?

From: Per Sundqvist

Date: 15 Nov, 2008 10:39:02

Message: 11 of 11

"Henry " <henry@fullspectrumengineering.com> wrote in message <gfkstu$1id$1@fred.mathworks.com>...
> I want to vectorize this simple snippet:
>
> for i=1:num_elements
> y(i)=my_matrix(row(i),col(i));
> end
>
> I tried y=my_matrix(row,col)
> and y=my_matrix([row col])
>
> but nothing seems to work. Any pointers? Thanks.

This works, but its not so obvious and simple as it should be

M=rand(4)
row=(1:3);
col=(2:4);
y=diag(M(row,col))
/Per

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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com