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:
Vectorization problem

Subject: Vectorization problem

From: Andreas

Date: 22 Oct, 2010 10:09:03

Message: 1 of 3

Hello everybody,

can you please help me to vectorize the following code:

R = zeros(oldnk,6);
for i = 1:oldnk
    R(i,:)=find(A(i,:));
end

A is an adjacency matrix and I want to transfer the indices of the non-zeros elements in a row to a matrix R.

Thank for you help.

Subject: Vectorization problem

From: Miroslav Balda

Date: 22 Oct, 2010 10:38:04

Message: 2 of 3

"Andreas " <snanreit@am.uni-erlangen.de> wrote in message <i9rnrv$o9j$1@fred.mathworks.com>...
> Hello everybody,
>
> can you please help me to vectorize the following code:
>
> R = zeros(oldnk,6);
> for i = 1:oldnk
> R(i,:)=find(A(i,:));
> end
>
> A is an adjacency matrix and I want to transfer the indices of the non-zeros elements in a row to a matrix R.
>
> Thank for you help.

Hi Andreas,
It is also possible to make indexing by logical variables. In that case R would be a matrix of logical values
   R = A~=0;
Should the (double!) indeces be needed, the following command would be used after the upper one:
  R = diag(1:size(A,1))*R;
Hope it helps.
Mira

   

Subject: Vectorization problem

From: Bruno Luong

Date: 22 Oct, 2010 11:45:05

Message: 3 of 3

"Andreas " <snanreit@am.uni-erlangen.de> wrote in message <i9rnrv$o9j$1@fred.mathworks.com>...
> Hello everybody,
>
> can you please help me to vectorize the following code:
>
> R = zeros(oldnk,6);
> for i = 1:oldnk
> R(i,:)=find(A(i,:));
> end
>
> A is an adjacency matrix and I want to transfer the indices of the non-zeros elements in a row to a matrix R.

Check out my submission:

http://www.mathworks.com/matlabcentral/fileexchange/24641

Bruno

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