help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: a difficult homework


From: Bård Skaflestad
Subject: RE: a difficult homework
Date: Thu, 22 Aug 2019 15:38:42 +0000

Alternatively, you could use ACCUMARRAY (with subscripts created by REPELEM) to apply MIN to groups of values extracted from 'c'.  The only slightly non-trivial bit is generating the indices into 'c', but there are very standard CUMSUM-based ways of doing so.

 

Regards,

 

Bård Skaflestad

 

SINTEF Digital, Mathematics & Cybernetics

Computational Geosciences group

 

From: Help-octave <help-octave-bounces+bard.skaflestad=address@hidden> On Behalf Of Nicholas Jankowski
Sent: Thursday, August 22, 2019 4:07 PM
To: mmuetzel <address@hidden>; shivax <address@hidden>; LucaLuca <address@hidden>
Cc: Help GNU Octave <address@hidden>
Subject: Re: a difficult homework

 

On Thu, Aug 22, 2019 at 8:55 AM mmuetzel <address@hidden> wrote:

If you really have to avoid the loop, you could try to use "arrayfun" with an
anonymous function (which kind of hides the loop).
I can't think of a straight-forward "non-loopy" solution of your problem
from the top of my head. I would be interested to see one, too.

Markus

I have a working solution, but is this a homework assignment?  I'm hesitant to just post the full code.  

 

The solution is almost always to spread the looped operation over an expanded array (here it can be kept to two dimensions. I've frequently made use of n-dimensional operations, and broadcast projection for this. for large data sets you have to be careful, as you're trading time for memory)

 

you're working with you c matrix as a single column of data, and looping 4 times, once over each a (and b, but they're looped together, so it's only one extra dimension).  you can make four parallel copies of the c matrix, and then apply the min function.  a common way to do quick array duplication is:

 

c = c(:);

cc = c .* ones(1,4);    

 

or more generally 

 

cc = c.* ones(1,numel(a));

 

then, you notice that min and max functions apply only in one dimension.  so min(cc) will give you the min of each column.  

 

the tricky part is that you aren't taking the min of the full column, just a portion of it.  so you'll need to figure out how to sample only a portion of each column, as specified in a and b, maybe using logical indexing, and take the min over that. this is complicated by it being a different length portion each time.  

 

 

 

 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]