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:
Indexing, modifying, then returning values

Subject: Indexing, modifying, then returning values

From: Anthony Hopf

Date: 18 May, 2010 13:22:04

Message: 1 of 7

Good morning,

Thank you again for your help. I get a bit confused when I try to vectorize the following "for" loops. The purpose of the loops is to index into P_4d_BB, grab out the correct values, modify the values, then put them back into the correct index where they came from. Index_beam_range is a 3d table that is indexed i,j,k where the values in index_beam_range(i,j,:) are the index values of interest in P_4d_BB. The "for" loops work but as my index_beam_range table gets larger it will obviously take much more time. P_3d_BB is a 3d matrix here P_3d_BB(m,n,:) is indexed by index_beam_range. The line for ibrt strips any NaN values in that particular (i,j,:) "vector" of index_beam_range.

for rb = 1:size(index_beam_range,2)%step through range bin table index
%second loop beam pointing
for bc = 1:size(index_beam_range,1)%step through the beam pointing angles

ibrt = index_beam_range(bc,rb,~isnan(index_beam_range(bc,rb,:)));
af = size(ibrt,3);
npw = sum(P_3d(ibrt))/af;
P_3d_BB(ibrt) = npw;
clear ibrt

Thank you,

Tony

end%beam pointing angle
end%range bin table index

Subject: Indexing, modifying, then returning values

From: Anthony Hopf

Date: 18 May, 2010 15:42:05

Message: 2 of 7

I feel like I must have written the problem in a confusing way. I thought this would be a really ez vectorizing problem to figure out. I have been staring at this since yesterday without any improvement...

Thanks

T

>
> for rb = 1:size(index_beam_range,2)%step through range bin table index
> %second loop beam pointing
> for bc = 1:size(index_beam_range,1)%step through the beam pointing angles
>
> ibrt = index_beam_range(bc,rb,~isnan(index_beam_range(bc,rb,:)));
> af = size(ibrt,3);
> npw = sum(P_3d(ibrt))/af;
> P_3d_BB(ibrt) = npw;
> clear ibrt
>
> Thank you,
>
> Tony
>

Subject: Indexing, modifying, then returning values

From: Anthony Hopf

Date: 19 May, 2010 10:37:05

Message: 3 of 7

Ok, so i've been thinking about this and it looks like to vectorize the for look I would need many temporary matrices to hold each value of index_beam_range.

Now I'm looking at parallelizing the problem. I have a quadcore I am working with so I could hope to split is locally 4 times. But I get an error:
??? Error: File: /home/otw/hopf/matlab/code/TEMP10/beambincorrect.m Line: 20 Column: 1
The variable P_4d_BB in a parfor cannot be classified.
 See <a href="matlab: helpview([docroot
 '/toolbox/distcomp/distcomp_ug.map'],'MIR_ERROR_PARFOR_BAD_TEMPORARY_VARIABLE')">Parallel for
 Loops in MATLAB, "Overview"</a>.

It looks like I can't slice up P_4d_BB because multiple points are indexed at a time... but because each internal for loop acts independently shouldn't I be able to parallelize it? I know I can't clear ibrt in the parfor, so i also took that out.

Any help?

Thank you,

Tony
>
> for rb = 1:size(index_beam_range,2)%step through range bin table index
> %second loop beam pointing
> for bc = 1:size(index_beam_range,1)%step through the beam pointing angles
>
> ibrt = index_beam_range(bc,rb,~isnan(index_beam_range(bc,rb,:)));
> af = size(ibrt,3);
> npw = sum(P_3d(ibrt))/af;
> P_3d_BB(ibrt) = npw;
>
> end%beam pointing angle
> end%range bin table index

Subject: Indexing, modifying, then returning values

From: Edric M Ellis

Date: 19 May, 2010 12:09:32

Message: 4 of 7

"Anthony Hopf" <anthony.hopf@gmail.com> writes:

> Ok, so i've been thinking about this and it looks like to vectorize
> the for look I would need many temporary matrices to hold each value
> of index_beam_range.
>
> Now I'm looking at parallelizing the problem. I have a quadcore I am working with so I could hope to split is locally 4 times. But I get an error:
> ??? Error: File: /home/otw/hopf/matlab/code/TEMP10/beambincorrect.m Line: 20 Column: 1
> The variable P_4d_BB in a parfor cannot be classified.
> See <a href="matlab: helpview([docroot
> '/toolbox/distcomp/distcomp_ug.map'],'MIR_ERROR_PARFOR_BAD_TEMPORARY_VARIABLE')">Parallel for
> Loops in MATLAB, "Overview"</a>.
>
> It looks like I can't slice up P_4d_BB because multiple points are
> indexed at a time... but because each internal for loop acts
> independently shouldn't I be able to parallelize it?

Could you post an example showing the actual PARFOR loop you're trying
to run which is failing?

Cheers,

Edric.

Subject: Indexing, modifying, then returning values

From: Anthony Hopf

Date: 19 May, 2010 12:49:05

Message: 5 of 7

Edric,

Sorry about that. It is the same code as above but the inner loop is the parfor. I have a feeling I can't run the parfor loop within another for loop. I need to reshape index_beam_range so I can write one for loop to step through the entire array... it will be i*j in length. But assuming we can reshape index_beam_range in this way... basically just one parfor loop the code would look like this:

P_4d_BB = P_4d;
aif = ones(size(P_4d)); %antenna integration factor (to be applied to informativeness)
%first loop range bin
parfor bc = 1:length(index_beam_range)%step through the beam pointing angles
ibrt = index_beam_range(bc,~isnan(index_beam_range(bc,:)));
aif(ibrt) = size(ibrt,3);
npw = nansum(P_4d(ibrt));
P_4d_BB(ibrt) = npw;
end%beam pointing angle



Edric M Ellis <eellis@mathworks.com> wrote in message <ytwwrv0nldv.fsf@uk-eellis-deb5-64.mathworks.co.uk>...
> "Anthony Hopf" <anthony.hopf@gmail.com> writes:
>
> > Ok, so i've been thinking about this and it looks like to vectorize
> > the for look I would need many temporary matrices to hold each value
> > of index_beam_range.
> >
> > Now I'm looking at parallelizing the problem. I have a quadcore I am working with so I could hope to split is locally 4 times. But I get an error:
> > ??? Error: File: /home/otw/hopf/matlab/code/TEMP10/beambincorrect.m Line: 20 Column: 1
> > The variable P_4d_BB in a parfor cannot be classified.
> > See <a href="matlab: helpview([docroot
> > '/toolbox/distcomp/distcomp_ug.map'],'MIR_ERROR_PARFOR_BAD_TEMPORARY_VARIABLE')">Parallel for
> > Loops in MATLAB, "Overview"</a>.
> >
> > It looks like I can't slice up P_4d_BB because multiple points are
> > indexed at a time... but because each internal for loop acts
> > independently shouldn't I be able to parallelize it?
>
> Could you post an example showing the actual PARFOR loop you're trying
> to run which is failing?
>
> Cheers,
>
> Edric.

Subject: Indexing, modifying, then returning values

From: Edric M Ellis

Date: 20 May, 2010 07:07:53

Message: 6 of 7

"Anthony Hopf" <anthony.hopf@gmail.com> writes:

> Sorry about that. It is the same code as above but the inner loop is
> the parfor. I have a feeling I can't run the parfor loop within
> another for loop. I need to reshape index_beam_range so I can write
> one for loop to step through the entire array... it will be i*j in
> length. But assuming we can reshape index_beam_range in this
> way... basically just one parfor loop the code would look like this:
>
> P_4d_BB = P_4d;
> aif = ones(size(P_4d)); %antenna integration factor (to be applied to informativeness)
> %first loop range bin
> parfor bc = 1:length(index_beam_range)%step through the beam pointing angles
> ibrt = index_beam_range(bc,~isnan(index_beam_range(bc,:)));
> aif(ibrt) = size(ibrt,3);
> npw = nansum(P_4d(ibrt));
> P_4d_BB(ibrt) = npw;
> end%beam pointing angle


It looks like you're trying to calculate "aif" and
"P_4d_BB". Unfortunately, the way you're indexing those is not
compatible with PARFOR - the output variables from a PARFOR loop must be
constructed either as "sliced" outputs, or "reduction" outputs. I think
"sliced" is most appropriate for what you're trying to do - a sliced
output variable basically means that you assign into it using the loop
variable (and possibly some other constant indices). For example, here's
an approximation to what I think you're trying to do:

data = rand(10);
parfor ii = 1:10
  transformedIdx = 11 - ii; % this is your "ibrt" calculation
  output( transformedIdx ) = sum( data( transformedIdx, : ) );
end

This fails because "output" is not sliced correctly. One way to fix
things is like this:

data = rand(10);
parfor ii = 1:10
  transformedIdx = 11 - ii; % this is your "ibrt" calculation
  % Store the mapping so it can be reversed later
  idxMap( ii ) = transformedIdx;
  % index into output using the loop variable "ii"
  output( ii ) = sum( data( transformedIdx, : ) );
end

% At this point, "output" is scrambled, but you can descramble it like
% so:
[y, i] = sort( idxMap );
output = output( i );

For more details, see the doc on sliced variables etc.:

<http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brdqtjj-1.html>

Cheers,

Edric.

Subject: Indexing, modifying, then returning values

From: Anthony Hopf

Date: 20 May, 2010 17:00:21

Message: 7 of 7

Edric,

Thank you for the response!! I will work on this...

Tony

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwocgbnj92.fsf@uk-eellis-deb5-64.mathworks.co.uk>...
> "Anthony Hopf" <anthony.hopf@gmail.com> writes:
>
> > Sorry about that. It is the same code as above but the inner loop is
> > the parfor. I have a feeling I can't run the parfor loop within
> > another for loop. I need to reshape index_beam_range so I can write
> > one for loop to step through the entire array... it will be i*j in
> > length. But assuming we can reshape index_beam_range in this
> > way... basically just one parfor loop the code would look like this:
> >
> > P_4d_BB = P_4d;
> > aif = ones(size(P_4d)); %antenna integration factor (to be applied to informativeness)
> > %first loop range bin
> > parfor bc = 1:length(index_beam_range)%step through the beam pointing angles
> > ibrt = index_beam_range(bc,~isnan(index_beam_range(bc,:)));
> > aif(ibrt) = size(ibrt,3);
> > npw = nansum(P_4d(ibrt));
> > P_4d_BB(ibrt) = npw;
> > end%beam pointing angle
>
>
> It looks like you're trying to calculate "aif" and
> "P_4d_BB". Unfortunately, the way you're indexing those is not
> compatible with PARFOR - the output variables from a PARFOR loop must be
> constructed either as "sliced" outputs, or "reduction" outputs. I think
> "sliced" is most appropriate for what you're trying to do - a sliced
> output variable basically means that you assign into it using the loop
> variable (and possibly some other constant indices). For example, here's
> an approximation to what I think you're trying to do:
>
> data = rand(10);
> parfor ii = 1:10
> transformedIdx = 11 - ii; % this is your "ibrt" calculation
> output( transformedIdx ) = sum( data( transformedIdx, : ) );
> end
>
> This fails because "output" is not sliced correctly. One way to fix
> things is like this:
>
> data = rand(10);
> parfor ii = 1:10
> transformedIdx = 11 - ii; % this is your "ibrt" calculation
> % Store the mapping so it can be reversed later
> idxMap( ii ) = transformedIdx;
> % index into output using the loop variable "ii"
> output( ii ) = sum( data( transformedIdx, : ) );
> end
>
> % At this point, "output" is scrambled, but you can descramble it like
> % so:
> [y, i] = sort( idxMap );
> output = output( i );
>
> For more details, see the doc on sliced variables etc.:
>
> <http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/brdqtjj-1.html>
>
> Cheers,
>
> Edric.

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