From: James Tursa on
Parker <xenoszh(a)gmail.com> wrote in message <04051753-dcd9-4f64-b8cd-315d2f03f29a(a)15g2000yqi.googlegroups.com>...
>
> the image was read as A = imread('test.jpg'), the A(:,:,1), A(:,:,2)
> and A(:,:,3) was the 3 components of R, G and B. due to I need to
> fetch the R, G, B values at a specified position within a rather long
> loop, I want to get them at the same time like [r,g,b]=A(i,j,:),
> unfortunately it didn't work.
>
> I think I need to try other ways to modify the algorithm to avoid the
> long loop.

If you really want to, the fastest way to get the r, g, b values separately is to use Walter's suggestion.

r = A(:,:,1);
g = A(:,:,2);
b= A(:,:,3);

Since the r,g,b values are each contiguous and are not interleaved, this will *not* cause the data to be traversed three times. The above is the fastest way to do it, a mex routine will not be any faster. But, other than convenience, there is no point in doing this if you are after speed improvements. Accessing r(i,j) and g(i,j) and b(i,j) won't be any faster than accessing A(i,j,1) and A(i,j,2) and A(i,j,3) ... they both get the three values from separate places in memory.

James Tursa