For Programmers: Free Programming Magazines  


Home > Archive > Matlab > July 2006 > efficient way to do this









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author efficient way to do this
Pinpress

2006-07-28, 7:04 pm

Hi,

I have an image with size (n-by-n). I need to change its intensity
value specified by locations (Y,X), where both X and Y are vectors,
and (Y(i),X(i)) specifies the (Yi)th row and (Xi)th column of the
image 2D array im. My initial thougt is to do this:

im(Y,X)=255;

However, it does not give the right result. What it does seems to be
this:

im( min(Y):max(Y), min(X):max(X) ) =255;

If I do the following, it works:

for i=1:length(Y)
im(Y(i),X(i)) = 255;
end

Anybody knows why this is happening, and what is a more efficient way
to achieve this without the for loop? Many thanks.
Shardul Bhatia

2006-07-28, 7:04 pm

Yes, that is how indexing works for Matrices.

Here is another way to get around it:

[r, c] = size(im)
im(X+r*(Y-1)) = 255

The above should work for you as Matrices can also be indexed with
one index. Let me know if you have any further questions.

Pinpress wrote:
>
>
> Hi,
>
> I have an image with size (n-by-n). I need to change its intensity
> value specified by locations (Y,X), where both X and Y are vectors,
> and (Y(i),X(i)) specifies the (Yi)th row and (Xi)th column of the
> image 2D array im. My initial thougt is to do this:
>
> im(Y,X)=255;
>
> However, it does not give the right result. What it does seems to
> be
> this:
>
> im( min(Y):max(Y), min(X):max(X) ) =255;
>
> If I do the following, it works:
>
> for i=1:length(Y)
> im(Y(i),X(i)) = 255;
> end
>
> Anybody knows why this is happening, and what is a more efficient
> way
> to achieve this without the for loop? Many thanks.

Jérôme

2006-07-31, 4:07 am

Hi,

help sub2ind
<http://www.mathworks.com/access/hel...ef/sub2ind.html>

Jérôme
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com