help-octave
[Top][All Lists]

## Re: using cellfunction

 From: Juan Pablo Carbajal Subject: Re: using cellfunction Date: Thu, 11 May 2017 23:49:51 +0200

```On Thu, May 11, 2017 at 11:47 PM, Juan Pablo Carbajal
> On Thu, May 11, 2017 at 10:42 PM, BOKU
>> Thank you for your response ,
>>
>> yes I do understand,
>>
>> but I did already "UniformOutput", false
>>
>> The problem is the function, as I want to apply a function which needs more
>> than just the matrix from the cell x{1,1} (for the first cell), no I need
>> furthermore the data which are stored in   x{3,1}  (for the first cell). And
>> It seems that I cannot index the matrix. I just dont know what cellfunction
>> does with the cell.. I mean if I do @mean or @(x) x-1 i could basicly do
>> mean(cell2mat(x)) and it's done! But if i have a {1,3} cell array in which
>> {1,1} is a matrix of let's say 300X300X300 and {1,2} is a 1X12 array, and
>> {1,3} is a an 200,120 array what happens then. but that is exactly what i
>> want to do I want to applay a function to  {1:end,1} with individual datas
>> stored in {1:end,3}. And idon't want to loop because it's incredible slow.
>> It takes about 20 min looping over this cell and I have several hundreds of
>> them. So I want to find another solution.
>>
>> x{1,i}=foundimfilt{i};
>>
>> x{2,i}=ref{i};
>>
>> x{3,i}=meanval{i};
>>
>>
>> function [y]= doit(x)
>>
>> idxl=x(1,1)(:,:,1)>x(3,1)(7)+0.3 | x(1,1)(:,:,1)<x(3,7)-0.3
>>
>> x(1,1)(:,:,2)>x(1,3)(6)+0.3 | x(1,1)(:,:,2)<x(1,3)(6)-0.3 ...
>>
>> &x(1,1)(:,:,3)>x(1,3)(8)+0.3 | x(1,1)(:,:,3)<x(1,3)(8)-0.3;
>>
>> idxl=imcomplement(idxl);
>>
>> pointsel=medfilt2(idxl,[10,10]);
>>
>> endfunction
>>
>>
>> pointselection = cellfun(@doit, x ,"UniformOutput" ,false );
>>
>>
>> On 2017-05-11 22:11, Juan Pablo Carbajal wrote:
>>
>> On Thu, May 11, 2017 at 9:41 PM, BOKU
>>
>> Hello,
>>
>> I would to apply a function to a 3x371 cell array
>>
>> so my question would if there is a solution to the following
>>
>> for i=1:numel(values)                              ####### wrapping elements
>> to get access in cellfunktion
>> ####### because cellfunction does just deliver the cellaray
>>    wrapobj{1,i}=foundimfilt{i};               #######  I hoped that it will
>> deliver the 3x1 cell
>>    wrapobj{2,i}=ref{i};
>>    wrapobj{3,i}=meanval{i};
>>
>> endfor
>>
>>
>> function [pointsel]= similarpoints(f)
>>
>>      idxl=f(1,1)(:,:,1)>f(3,1)(7)+0.3 | f(1,1)(:,:,1)<f(3,7)-0.3&
>> f(1,1)(:,:,2)>f(1,3)(6)+0.3 | f(1,1)(:,:,2)<f(1,3)(6)-0.3 ...    ####### so
>> that i can access the data in the &f(1,1)(:,:,3)>f(1,3)(8)+0.3 |
>> f(1,1)(:,:,3)<f(1,3)(8)-0.3; ####### function
>>
>> idxl=imcomplement(idxl);
>> pointsel=medfilt2(idxl,[10,10]);
>> endfunction
>>
>>
>> pointselection = cellfun(@similarpoints, wrapobj,"UniformOutput" ,false );
>> ############ but unfortunately it seems that i don't
>> get the full  cell
>>
>>
>> error: f(7): out of bound 1
>> error: called from
>>     similarpoints at line 3 column 10
>>
>>
>> is there a solution for the indexing or the delivering the needed
>> informations for the function. Basicly i want to apply an individual filter
>> to every cell and I need the information stored in wrapobj{3,i} to apply it
>> on the matrix stored in wrapobj{1,i}!
>>
>> wrapobj is a 3x371 cell array.
>>
>> i would appreciate any help on this
>>
>> cheers chris
>>
>>
>>
>>
>> _______________________________________________
>> Help-octave mailing list
>> https://lists.gnu.org/mailman/listinfo/help-octave
>>
>> Hi,
>> cellfun respects the size of the input, that is
>>
>> c = {1,4,7,10;2,5,8,11;3,6,9,12};
>> f=@(x) x-1;
>> cp = cellfun (f,c)
>>
>> cp =
>>
>>     0    3    6    9
>>     1    4    7   10
>>     2    5    8   11
>>
>> If you input is a 3x371 cell, then the output of cellfun will be a
>> cell of the same size. If the output of the function you evaluate on
>> each cell element is an array or cell, then you should tell cellfun
>> that 'UniformOutput' is false, e.g.
>>
>> f=@(x) linspace(1,x,3);
>> cp = cellfun (f,c,'UniformOutput', false);
>> whos cp
>>
>> Variables in the current scope:
>>
>>    Attr Name        Size                     Bytes  Class
>>    ==== ====        ====                     =====  =====
>>         cp          3x4                        288  cell
>>
>> Total is 12 elements using 288 bytes
>>
>>
>
> cellfun, arrayfun and similar functions apply the function to each
> element of the cell. so if the function can process all the elements
> in the cell, e.g. different sized matrices, scalars, strings, etc..
> then cellfun will just apply it to each element. It is the
> generalization of the "element-wise" behavior of functions applied to
> arrays, i.e. sin, tanh, exp, abs, etc...
>
> But cellfun can also work in two cells, advancing on each other
> simultaneously and implement element-wise functions of many inputs
> (n-arity), for you case you could do
>
> cellfun (your_func, your_cell(1,:), your_cell(3,:), 'unif', 0)
>
> Does it work?

you can also do arrayfun on the index...

index = 1:size(your_cell,2);
arrayfun (@(i)your_func(your_cell{1,i},your_cell{3,i}), index, 'unif', 0)

```