help-octave
[Top][All Lists]

## Re: a difficult homework

 From: Nicholas Jankowski Subject: Re: a difficult homework Date: Thu, 22 Aug 2019 10:06:35 -0400

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