About cellfun / arrayfun with accessing indices

Hi folks, i’m new here and don’t know doing the right to ask / propose a feature of cellfun / arrayfun.
The last times while programming in octave i would have a great benefit, if i would have access to the act index in cellfun / arrayfun:

cellfun(@(x,y) ones(1,1:numel(x)*y, cell,‘uniformoutput’,false).
I have search the web of this feature, but don’t get any info at all.
It would be nice, if the ‘y’ was holding the actual index, like [1,2,1] or similar.
My workaround for one-dimensional cells is like that:

persistentIdx(true);
out=cellfun(@(x) ones(1:numel(x))*persitentIdx, cell, ‘uniformoutput’, false),

were the persitentIdx is a function like that:

function idx=persistentIdx(start=false)
… persitent pIdx=0;

… if start
… … pIdx=0
… else
… … pIdx+=1;
… endif

… idx=pIdx
endfunction;

So i think, it would be nice to have access to the actual index. I don’t like loops, so i tend to this workaround. Maxbe, there exists another solution for that what want do.

If my post isn’t on the right place, i’m sorry about. Hope, my question/suggestion is clear.
I’m wondering, that index isn’t accessible within the cellfun. Otherwise i’m sure, it gives a reason vor that.

ps: Is there a code - editor anyway?

I’m not sure if I understand your goal correctly.
Maybe you’d like to do something like the following?

a = {1, 1:2, 1:5};
out = cellfun (@(x,y) ones(1, numel (x)) * y, a, num2cell ((1:numel(a))-1), "UniformOutput", false);

For 2 dimensional cell arrays at least, you can generate a 2 dimensional matrix of indices using meshgrid.

x = {'a', 'b', ; 'c', 'd'; 'e', 'f'}
[yy, zz] = meshgrid (1:2, 1:3)
cellfun(@(x,y,z)fprintf('%d,%d : %c\n', z,y,x), ...
x,num2cell(yy),num2cell(zz))

Thank you very much!

The goal was to calculate a cellidx(strfind) result. I wanted to sort the strfind-result by position, for example cellstr looks like:

indent preformatted text by 4 spaces

{
[1,20,30],
,
[10,25,40],
[35]
}

My idea was (and it works :slight_smile: ) to give the result [pos,idx,{str}]= myStrfind odered by any of them:
[ [1,10,20,25,30,35,40] , [1,3,1,3,1,4,3], [“aba”,“foo”,“bar”] ]. To do that, i’d have to number consecutively
the strfind result.
->[ 1, 1 ,1 ,3 ,3 ,3 ,4]
To do this, i’ve made the workarround. If i’ve understand you right, it’s work like

cellfun(@(x,y,z) fcn(x,y,z) , xcell , ycell , zcell)

Thank you so much for teach me this! I’d found the cellfun / arrayfun very powerful, once understanding it :slight_smile:
it replaced a lot of loop-codes and is faster. I did not know anything about the use of the function in your sense. Until a couple of weeks ago I had only heard about, but haven’t make use of them. It’s very beauty powerful stile i’ve think.
I hadn’t found anything in the web or octave-docs for this functionality. There are so many functions in octave - every time a new, sometimes not dokumented. This new known functionality seems very, very powerful!
Yet it’s time to discover.

PS: I’m out of germany, sorry for my english-stile, hope you understand.I’m already open for language tutoring too :wink:

Also thank’s to you! This code you send seem to make it very clearly, how to use cellfun. The longer I deal (or better “mess” | “take use of” ?) with octave, the more I become a fan :slight_smile:

Have it done (hopefully) Very nice … Thanks.

For you it is definitely not difficult to say which is faster:
Loop or persistent call.
But i guess, the loop is faster. I will profiling this.

I’m amazed about the profiling results.

The Algorithm follows:

outArray=[cellfun(@(x,y) ones(1,length(x))*y,inCell,num2cell(1:length(inCell)),‘uniformoutput’,false){:}];

   Input: {  1
             1, 2

             1, 2, 3,  ... n  }

   Output: { 1
             2, 2
               
             n times n  }

Only one loop over the rows - clearly

Cellfun2(n=25);
Number of elements: 325
ans = persistent:
ans = 6.7496e-04
ans = loop:
ans = 4.3392e-04
ans = cellfun
ans = 4.1699e-04

Cellfun2(n=250);
Number of elements: 31375
ans = persistent:
ans = 5.7180e-03
ans = loop:
ans = 5.3871e-03
ans = cellfun
ans = 3.1290e-03

Cellfun2(n=2500);
Number of elements: 3126250
ans = persistent:
ans = 0.068603
ans = loop:
ans = 3.0926
ans = cellfun
ans = 0.044662

I guess it’s the internal memory-allocation, what makes the loop so slow. Is it right?
But why is the persistence-method so fast?

With initialize the whole array, the loop works faster then the cellfun.