Matlab indexing into 2D array using

I have a 2D matrix, say M=zeros(10,10);

I have another column matrix, V=[1;2;3;4;5;6;5;4;3;2];

I would like to be able to set M(i,j) = 1 for all j >= V(i)

I know I can do this in a loop

for i=1:10
   M(i,V(i):10) = 1;
end

but it seems that it would be possible to use some form of Matlab indexing to avoid using a loop. For example something like :

M(:,V:10)=1;

or

M(:,V(:):10)=1;

but neither of these produces the expected result.

Is there some syntactic sugar I can use to achieve this or should I revert to looping?

Answers


Since you're looking for syntactic sugar, here's a sort of esoteric way of doing it. :)

Assuming the length of V is the size of both dimensions in the desired matrix M, first create an identity matrix of the same size, then index appropriately and take cumsum:

V = [1;2;3;4;5;6;5;4;3;2]; #% 10x1 vector
E = eye(length(V), length(V)); #%10x10 identity matrix
M = cumsum(E(V,:),2)

M =

     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1

Ok, now: less fun, but (on my machine) faster than any of the other options that have been tested so far:

n=10000;
V = randi(n-1, 1, n); #% as in @KevinRatelle's answer (but not transposed)

tic;
Vlinear = reshape(V + (0:n-1)*n, 1, []); #% find linear indices of first "ones"
M = zeros(n);
M(Vlinear)=1;
M=cumsum(M);
toc

Its hardly subtle and not really better than a loop I don't think but:

[J,I] = meshgrid(1:10,1:10); 
V = [1;2;3;4;5;6;5;4;3;2];
M = J>V(I);

Enjoy.


I tried the loop method, and the 'meshgrid' method. I was wondering about the time to compute for large matrices (since the issue with loops in matlab is usually the time).

First, I optimised the code to look like this :

V = V*ones(1,n);
N = ones(1,n)'*(1:n);
M = N>=V;

Actually, N is a meshgrid, but it seems way faster to do it this way...

I tried this :

n = 10000;
V = randi(n-1,1,n)';

tic;
M = zeros(n);
for i=1:n
    M(i,V(i):n) = 1;
end
toc

tic;
[J,I] = meshgrid(1:n,1:n);
M = J>=V(I);
toc

tic;
V = V*ones(1,n);
N = ones(1,n)'*(1:n);
M = N>=V;
toc

And results were :

Elapsed time is 1.726872 seconds.
Elapsed time is 5.206657 seconds.
Elapsed time is 1.548600 seconds.

But methods using matrices instead of loops are memory consuming for large n. I would personnaly stick to the loop.


try this:

v = [1;2;3;4;5;6;5;4;3;2];
n = 10;
M = repmat((1:n)', 1, numel(v)) > repmat(v', n, 1);

Need Your Help

CSS3PIE element corners with different radii using border-radius?

frontend css3 css3pie

I have an element where the top-left and bottom-right corners have a border-radius of 5 and I'm trying to make it compatible with CSS3PIE. The documentation states that

dynamic div rendering in jquery. cursor moves to start on reload of div

jquery html cross-browser

HI I have the jquery script which calls the server every 5 seconds and reload the content (via synchronous ajax call) into div which has a scroll bar enables if the content of the div overflows. no...