From: A on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <ho822l$709$1(a)fred.mathworks.com>...
> "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.

Which circumstances does it not work under?
From: A on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <ho82r0$kig$1(a)fred.mathworks.com>...
> Generalize OP's original solution to nd:
>
> c=num2cell(coords,1)
> M(sub2ind(max(coords,[],1),c{:})) = 255
>
> Bruno % still prefer accumarray

except in my specific case (size of M is predetermined)
M(sub2ind(size(M), c{:})) = 255
but thanks, this is useful too. I've never really warmed up to cell arrays, but they seem to have some interesting uses. Anyway, accumarray FTW.
From: Matt Fig on
"A " <aaarbk(a)geemail.com> wrote in message
> Which circumstances does it not work under?

The accumarray soln leaves elements not in coords as zero. From your request, it sounded to me like your M wasn't *necessarily* all zeros, only your example was. If M is all zeros, then you can still get the correct size from accumarray by specifying the size as the third argument.
From: A on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <ho860u$fsn$1(a)fred.mathworks.com>...
> "A " <aaarbk(a)geemail.com> wrote in message
> > Which circumstances does it not work under?
>
> The accumarray soln leaves elements not in coords as zero. From your request, it sounded to me like your M wasn't *necessarily* all zeros, only your example was. If M is all zeros, then you can still get the correct size from accumarray by specifying the size as the third argument.

The application I have in mind currently does use a matrix M with all zeros, but I see your point. I had been under the misimpression that M was being supplied to accumarray as an argument, so hadn't realised the need to supply the size of M either, as you showed in your reply.

Anyway, I'll just use the following modification to get the desired functionality.

function A = assignarray(A, coords, vals);
M = accumarray(coords, vals, size(A));
A(M~=0) = M(M~=0);

Thank you both for your help.
From: Matt Fig on
"A " <aaarbk(a)geemail.com> wrote in message
> The application I have in mind currently does use a matrix M with all zeros, but I see your point. I had been under the misimpression that M was being supplied to accumarray as an argument, so hadn't realised the need to supply the size of M either, as you showed in your reply.
>
> Anyway, I'll just use the following modification to get the desired functionality.
>
> function A = assignarray(A, coords, vals);
> M = accumarray(coords, vals, size(A));
> A(M~=0) = M(M~=0);
>
> Thank you both for your help.

Creating M is unnecessary here if it's only purpose is as an index into A, which is why I said that you had it as simple as it gets in my first post!
Now you might be able to get a little speed by inlining sub2ind, but I doubt it will be much.

function A = assignarray2(A, coords, vals)
A(coords(:,1)+(coords(:,2)-1)*size(A,1)) = vals;