From: Al on
Hello All
two parts to the query:
1.
if I have a cellarray of say 1 x 500 which we will call B, and in each cell is a cell matrix of nx1 numbers (n is likely different for each column) call these B_i, and I have a double matrix 1 x 500 called A, containing a different number for each element.

I would like to calculate the largest number in each B_i which is less than or equal to the corresponding number in A (where corresponding means we look at the same column number each time).
eg
B = {29x1cell 20x1cell 60x1cell}
B{1,1}= {1 15 35 99 ...}
A=[40 60 54 ...]

so in this example the resultant matrix would have as the first entry the result of: B( max( ( cell2mat(B{1})<=A(1) ).' .* 1:numel(B{1}) ) )
or 35. but the construct I have here seems to only be suitable for a loop, Is it possible to nest cellfuns somehow?

2.
After another process if I have a logical array of zeros and ones, with say 500 rows x 500 columns. If I have a rw index specific to each column and I would like to sum up the logicals in each column from the first row index to the (rwindex -1)th row.
All I need is the total sum at the end, the intermediate sum would be interesting but not essential. Is this possible without a loop?
From: Steven Lord on

"Al " <alek.maxwell(a)eon.com> wrote in message
news:htj66b$l2h$1(a)fred.mathworks.com...
> Hello All
> two parts to the query:
> 1.
> if I have a cellarray of say 1 x 500 which we will call B, and in each
> cell is a cell matrix of nx1 numbers (n is likely different for each
> column) call these B_i, and I have a double matrix 1 x 500 called A,
> containing a different number for each element.
>
> I would like to calculate the largest number in each B_i which is less
> than or equal to the corresponding number in A (where corresponding means
> we look at the same column number each time). eg
> B = {29x1cell 20x1cell 60x1cell}
> B{1,1}= {1 15 35 99 ...}
> A=[40 60 54 ...]
>
> so in this example the resultant matrix would have as the first entry the
> result of: B( max( ( cell2mat(B{1})<=A(1) ).' .* 1:numel(B{1}) ) ) or
> 35. but the construct I have here seems to only be suitable for a loop, Is
> it possible to nest cellfuns somehow?

You know, FOR loops are not inherently evil -- for a problem as small as
this one, just preallocate the result vector and loop over the elements of
A. You'd likely spend more time trying to develop the non-loop solution
than it would take the loop solution to run, and the loop solution would
likely be easier to understand if you need to reuse it six months from now.

Yes, I know that the general rule used to be to avoid FOR loops whenever
possible. But MATLAB has changed, and used wisely FOR loops are just as
good a tool (in the right situation) as any.

> 2.
> After another process if I have a logical array of zeros and ones, with
> say 500 rows x 500 columns. If I have a rw index specific to each column
> and I would like to sum up the logicals in each column from the first row
> index to the (rwindex -1)th row.
> All I need is the total sum at the end, the intermediate sum would be
> interesting but not essential. Is this possible without a loop?

You can use TRIU and SUM.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Al on
Thanks Steven, I will move ahead with the for loop though it is a program I will likely use repeatedly so thought arrays may be the way to go.
For the second query I need to sum up the logicals for each column above a certain row where that row differs by column. so maybe first 20 rows of first column first 5 rows of second column etc, is there a way to get triu to accept a different diag offset for each column?