Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem using logical indexing within a for loop

Subject: Problem using logical indexing within a for loop

From: Thomas

Date: 30 Jan, 2014 13:12:20

Message: 1 of 3

Hi
I am having a problem implementing logical indexing within a for loop. I will illustrate this with a synthetic example:
Let's say we have a row vector containing a list of values and a matching binary row vector which contains the locations of values to be replaced with NaNs in the first vector:

myarray = randi(10,1,10) % to be updated
myarray =

     8 1 3 1 1 9 7 4 10 1

logi = round(rand(1,10)) % binary
logi =

     0 0 1 1 0 0 0 1 1 1

To update the first array I would normally do:

myarray(logical(logi)) = NaN
myarray =

     8 1 NaN NaN 1 9 7 NaN NaN NaN

However, I need to update n row vectors contained within an nx1 cell array, which I cannot get to work due to the need place extra parenthesis within my code to index each cell:

for i = 1: size(myarraycell,1)
     myarraycell{i} = randi(10,1,10);
     logi = round(rand(1,10));
     myarray{i}(logical(logi)) = NaN;
end

I can get around this by cloning the contents of myarraycell{i} within each loop, updating this and then copying in back to the cell array:

for i = 1: size(myarraycell,1)
     myarraytemp = myarraycell{i}; % create clone
     logi = round(rand(1,10));
     myarraytemp(logical(logi)) = NaN;
     myarraycell{i} = myarraytemp; % update cell array with clone
end

but this will entail a significant overhead (I am need to update several cell arrays containing several million cells). My real data is non rectangular and thus must be dealt with in a loop so I cannot get around this with further vectorisation.
Any suggestions how to solve this would be greatly appreciated.
Thanks
Thomas

Subject: Problem using logical indexing within a for loop

From: dpb

Date: 30 Jan, 2014 15:14:31

Message: 2 of 3

On 1/30/2014 7:12 AM, Thomas wrote:
> Hi
> I am having a problem implementing logical indexing within a for loop. I
> will illustrate this with a synthetic example:
> Let's say we have a row vector containing a list of values and a
> matching binary row vector which contains the locations of values to be
> replaced with NaNs in the first vector:
>
...

> To update the first array I would normally do:
>
> myarray(logical(logi)) = NaN
...

> However, I need to update n row vectors contained within an nx1 cell
> array, which I cannot get to work due to the need place extra
> parenthesis within my code to index each cell:
>
> for i = 1: size(myarraycell,1)
> myarraycell{i} = randi(10,1,10);
> logi = round(rand(1,10));
> myarray{i}(logical(logi)) = NaN;
> end
>
> I can get around this by cloning the contents of myarraycell{i} within
> each loop, updating this and then copying in back to the cell array:
>
...

I don't see the problem...your expression above should work just fine
unless my old eyes aren't distinguishing curlies and parens correctly or
there's something else you're not telling us...

 >> c={randi(10,1,10)}; c{1}
ans =
      5 1 3 10 2 9 6 10 1 5
 >> c{1}(c{1}>5)=nan; c{1}
ans =
      5 1 3 NaN 2 NaN NaN NaN 1 5
 >>

You can replace the logical test expression with whatever yours is and
should be just fine. The first reference uses the curlies to
dereference the cell content and the normal parens address those
elements. It in essence does the cloning transparently at the source
code level; I've not explored how much actual copying is managed to be
eliminated by the jit compiler but it does save an extra explicit store
certainly.

Also remember there's

doc cellfun

which may eliminate the explicit loop need depending on the actual
problem...

--

Subject: Problem using logical indexing within a for loop

From: Thomas

Date: 31 Jan, 2014 00:01:08

Message: 3 of 3

Thanks for your response dqb. You are totally correct. It was a syntax error behind my code not working which is why the synthetic example worked fine. I will look into using cellfun BTW: I didn't realise this could be called with custom functions.
Thanks again
Thomas

Tags for this Thread

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.

Contact us