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

New to MATLAB?

Question about assigning value in a matrix.

Asked by C Zeng

C Zeng (view profile)

on 6 Jun 2013

Hi, all, again, I want to do the work quickly: I have a large matrix A, and I want to assign value in some entry in each row.

For example:
A=zeros(4);
a=[1,2;
 2,3;
 3,2;
 4,1];

I want (1,2), (2,3),(3,2), (4,1) of matrix A be 1. How shall I make it work in no more than 3 commands?

What I am doing now is using loops:

for i=1:4
A(a(i,1),a(i,2)=1;
end

However, as i is very large like 3^14, the program is very slow. So I wonder is there a way to speed the programming?

Thanks.

0 Comments

C Zeng

C Zeng (view profile)

Products

No products are associated with this question.

3 Answers

Answer by Walter Roberson

Walter Roberson (view profile)

on 6 Jun 2013
Accepted answer
idx = sub2ind(size(A), [1 2 3 4], [2 3 2 1]);
A(idx) = 1;

3 Comments

C Zeng

C Zeng (view profile)

on 6 Jun 2013

Thanks, Walter! So sub2ind can find the index of A quickly by loops, right? I will definitely try this code. I like it!

Sean de Wolski

Sean de Wolski (view profile)

on 6 Jun 2013

I've always found for-loops to be significantly faster than sub2ind and ind2sub. Especially as the number of indices gets large.

It's been a few years since I was timing it for my own work though...

C Zeng

C Zeng (view profile)

on 6 Jun 2013

Oh, really? I always found that MATLAB is not efficient for large loops. That is why I tried to avoid large loops.

Walter Roberson

Walter Roberson (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 6 Jun 2013
A=zeros(4);
a=[1,2;
2,3;
3,2;
4,1];
idx= sub2ind(size(A),a(:,1),a(:,2));
A(idx)=1

1 Comment

C Zeng

C Zeng (view profile)

on 6 Jun 2013

Yes, Azzi! Thanks.

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Sean de Wolski

Sean de Wolski (view profile)

on 6 Jun 2013

I've always found for-loops to be significantly faster than sub2ind and ind2sub. Especially as the number of indices gets large.

It's been a few years since I was timing it for my own work though...

12 Comments

Sean de Wolski

Sean de Wolski (view profile)

on 7 Jun 2013

Well a few things real quick:

prMatrix = diag(pr);

Skips the first for-loop and call to zeros()

Second, you migh twant to consider taking the outer for-loop and converting it to a parfor with the Parallel Computing Toolbox. This would allow this to work in Parallel, over a few workers and speed things up that way.

Alternatively, just run it over the weekend or overnight, at two times total and 556 seconds, that's still only 20 minutes...

C Zeng

C Zeng (view profile)

on 7 Jun 2013

Thanks, Sean!

Let me try it again. I have to run large data on server, because 3^12 is memory out here.

C Zeng

C Zeng (view profile)

on 13 Jun 2013

Sean, thanks for your advice. My program got speedup. sub2ind will be faster if the size is large.

Sean de Wolski

Sean de Wolski (view profile)

Contact us