Thread Subject: error with "Indexing cannot yield multiple results"

Subject: error with "Indexing cannot yield multiple results"

From: Parker

Date: 19 Feb, 2010 23:13:48

Message: 1 of 6

after reading an image to a variable A, I met an error of "Indexing
cannot yield multiple results" when I want to assign the r, g, b
components to 3 varaiables like:

[r,g,b] = A(1,1,:)
==================
due it's a long loop, so I want to save the r,g,b components to speed
up the function.
Is there any way for me to do that?

"r = A(1,1,:); g=A(1,1,2);b=A(1,1,3);" is not the answer I want,
because it has to fetch values from the image array 3 times.

Thanks in advance.

Subject: error with "Indexing cannot yield multiple results"

From: James Tursa

Date: 19 Feb, 2010 23:21:04

Message: 2 of 6

Parker <xenoszh@gmail.com> wrote in message <764ee50d-dcdb-452e-aa43-a3b06481669d@g19g2000yqe.googlegroups.com>...
> after reading an image to a variable A, I met an error of "Indexing
> cannot yield multiple results" when I want to assign the r, g, b
> components to 3 varaiables like:
>
> [r,g,b] = A(1,1,:)
> ==================
> due it's a long loop, so I want to save the r,g,b components to speed
> up the function.
> Is there any way for me to do that?
>
> "r = A(1,1,:); g=A(1,1,2);b=A(1,1,3);" is not the answer I want,
> because it has to fetch values from the image array 3 times.
>
> Thanks in advance.

How is the r-g-b info actually stored? Your example code doesn't make sense to me. Are the r,g,b values interleaved in memory? If so, then a simple c-mex routine can generate the separate variables by accessing the original array only once. Let me know.

James Tursa

Subject: error with "Indexing cannot yield multiple results"

From: Walter Roberson

Date: 19 Feb, 2010 23:18:25

Message: 3 of 6

Parker wrote:
> after reading an image to a variable A, I met an error of "Indexing
> cannot yield multiple results" when I want to assign the r, g, b
> components to 3 varaiables like:
>
> [r,g,b] = A(1,1,:)
> ==================
> due it's a long loop, so I want to save the r,g,b components to speed
> up the function.
> Is there any way for me to do that?
>
> "r = A(1,1,:); g=A(1,1,2);b=A(1,1,3);" is not the answer I want,
> because it has to fetch values from the image array 3 times.

I could provide code that could do it, but it would be less efficient than the
individual assignments.

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

When the first two dimensions are allowed to vary and the third is fixed, then
the data extracted comes from a sequential block of memory and would be
written in a sequential block in exactly the same order -- the most efficient
memory copying operation that there is.

The alternatives that we could provide could do the assignments to [r,g,b] in
a single line, but for syntactic reasons a temporary variable would be
required, and it would be necessary to copy the original data into the
temporary variable.

Subject: error with "Indexing cannot yield multiple results"

From: Parker

Date: 20 Feb, 2010 08:53:10

Message: 4 of 6

On Feb 19, 11:21 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> Parker <xeno...@gmail.com> wrote in message <764ee50d-dcdb-452e-aa43-a3b064816...@g19g2000yqe.googlegroups.com>...
> > after reading an image to a variable A, I met anerrorof "Indexing
> > cannot yield multiple results" when I want to assign the r, g, b
> > components to 3 varaiables like:
>
> > [r,g,b] = A(1,1,:)
> > ==================
> > due it's a long loop, so I want to save the r,g,b components to speed
> > up the function.
> > Is there any way for me to do that?
>
> > "r = A(1,1,:); g=A(1,1,2);b=A(1,1,3);" is not the answer I want,
> > because it has to fetch values from the image array 3 times.
>
> > Thanks in advance.
>
> How is the r-g-b info actually stored? Your example code doesn't make sense to me. Are the r,g,b values interleaved in memory? If so, then a simple c-mex routine can generate the separate variables by accessing the original array only once. Let me know.
>
> James Tursa

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.

Subject: error with "Indexing cannot yield multiple results"

From: Parker

Date: 20 Feb, 2010 09:07:04

Message: 5 of 6

On Feb 19, 11:18 pm, Walter Roberson <rober...@hushmail.com> wrote:
> Parker wrote:
> > after reading an image to a variable A, I met anerrorof "Indexing
> > cannot yield multiple results" when I want to assign the r, g, b
> > components to 3 varaiables like:
>
> > [r,g,b] = A(1,1,:)
> > ==================
> > due it's a long loop, so I want to save the r,g,b components to speed
> > up the function.
> > Is there any way for me to do that?
>
> > "r = A(1,1,:); g=A(1,1,2);b=A(1,1,3);" is not the answer I want,
> > because it has to fetch values from the image array 3 times.
>
> I could provide code that could do it, but it would be less efficient than the
> individual assignments.
>
> r = A(:,:,1); b = A(:,:,2); g = A(:,:,3);
>
> When the first two dimensions are allowed to vary and the third is fixed, then
> the data extracted comes from a sequential block of memory and would be
> written in a sequential block in exactly the same order -- the most efficient
> memory copying operation that there is.
>
> The alternatives that we could provide could do the assignments to [r,g,b] in
> a single line, but for syntactic reasons a temporary variable would be
> required, and it would be necessary to copy the original data into the
> temporary variable.

thx, for optimizing purpose, it's better to do the assignments in
separate line, if there's no way to avoid getting values for 3 times.

Subject: error with "Indexing cannot yield multiple results"

From: James Tursa

Date: 20 Feb, 2010 16:31:02

Message: 6 of 6

Parker <xenoszh@gmail.com> wrote in message <04051753-dcd9-4f64-b8cd-315d2f03f29a@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

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com