Cellfun Index

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Cellfun Index

Mike Bloom
Hi, I'm using cellfun to use one cell which contains indexes to index from another cell.  My code is like this:

cellData = cell(1,2);
cellIndex = cell(1,2);

cellData{1} = [10:-1:1];
cellData{2} = [5:-1:1];

cellIndex{1} = [2,4,5];
cellIndex{2} = [2];

cellResult = cellfun(@(a,b) a(b), cellData, cellIndex, "UniformOutput", false)

And the result is this:

cellResult =
{
  [1,1] =

     9   7   6

  [1,2] =  4
}

This is the correct result.  My question is, is there a function available so that I can use cellfun without the anonymous function call?  I've tried looking through the function index, but wasn't able to find anything.  I want to avoid the anonymous function in order to try to speed the code up.

Thanks

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Cellfun Index

Pantxo
Mike Bloom wrote

> Hi, I'm using cellfun to use one cell which contains indexes to index from
> another cell.  My code is like this:
>
> cellData = cell(1,2);
> cellIndex = cell(1,2);
>
> cellData{1} = [10:-1:1];
> cellData{2} = [5:-1:1];
>
> cellIndex{1} = [2,4,5];
> cellIndex{2} = [2];
>
> cellResult = cellfun(@(a,b) a(b), cellData, cellIndex, "UniformOutput",
> false)
>
> And the result is this:
>
> cellResult =
> {
>   [1,1] =
>
>      9   7   6
>
>   [1,2] =  4
> }
>
> This is the correct result.  My question is, is there a function available
> so that I can use cellfun without the anonymous function call?  I've tried
> looking through the function index, but wasn't able to find anything.  I
> want to avoid the anonymous function in order to try to speed the code up.
>
> Thanks
>
> _______________________________________________
> Help-octave mailing list

> Help-octave@

> https://lists.gnu.org/mailman/listinfo/help-octave

Hi,

I think "subsref" is the functional form of indexing you are looking for,
but you need to prepare the indices in a different way. Something like


cellData = cell(1,2);
cellIndex = cell(1,2);

cellData{1} = [10:-1:1];
cellData{2} = [5:-1:1];

cellIndex{1} = struct ("type", "()", "subs", {{":", [2,4,5]}});
cellIndex{2} = struct ("type", "()", "subs", {{":", 2}});

cellResult = cellfun(@subsref, cellData, cellIndex, "UniformOutput", 0)


Pantxo





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave