From: A on
Hi,

I have an array of size m x 2, where each row represents the discrete (x,y) coordinates of a 2-d point, for a total of m points.
So coords = [1 2; 5 4; 8 9]
represents the points (1,2), (5,4) and (8,9) respectively.
I have a 2d-matrix M of size greater than max(coords), and I want to assign a single fixed number (say 255) to every point in the coords matrix. Right now I'm using sub2ind, so for instance
M = zeros(10);
M(sub2ind(size(M), coords(:,1), coords(:,2))) = 255
[of course this also works in the expected way if instead of assigning a fixed number we assign an array of size(coords, 1)]

but I can't help thinking there might be a more elegant way to do this in MATLAB, where each row of coords is directly interpreted as a 2-d index into M (and extends to n-dimensions, i.e. coords is of size mxn and M is n-dimensional matrix).

Is there?

Regards,
AK
From: Bruno Luong on
"A " <aaarbk(a)geemail.com> wrote in message <ho80m7$atc$1(a)fred.mathworks.com>...

>
> Is there?

M = accumarray(coords,255)

Bruno
From: Matt Fig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <ho81e0$o60$1(a)fred.mathworks.com>...
> "A " <aaarbk(a)geemail.com> wrote in message <ho80m7$atc$1(a)fred.mathworks.com>...
>
> >
> > Is there?
>
> M = accumarray(coords,255)
>
> Bruno


That will only work under special circumstances. I think the OP has it about as simple as it gets.
From: Bruno Luong on
Generalize OP's original solution to nd:

c=num2cell(coords,1)
M(sub2ind(max(coords,[],1),c{:})) = 255

Bruno % still prefer accumarray
From: A on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <ho81e0$o60$1(a)fred.mathworks.com>...
> "A " <aaarbk(a)geemail.com> wrote in message <ho80m7$atc$1(a)fred.mathworks.com>...
>
> >
> > Is there?
>
> M = accumarray(coords,255)
>
> Bruno

Thanks... I don't think I've ever noticed this function before. Does exactly what I had in mind, and works for vector assignment as well, eg. M = accumarray(coords, [10 20 30]) works for size(coords,1) = 3