Clear Filters
Clear Filters

Using arrayfun on the indices of a cell array to calculate all cells in one call?

3 views (last 30 days)
I have two cell arrays. One called Xcell which is of size 25x9 where each cell element is a design matrix for example of size 3600x664, but it can vary from row to row of the cell array (but always the same along the columns).
The second cell array, Ycell, is of size 1x9 where each cell element is the response variable to the Xcell elements.
I want to solve all the X\Y systems as quick as possible. First I define a function to solve each Xcell by using its corresponding Ycell like this:
myfun = @(sample,folds) Xcell{sample,folds}\Ycell{1,folds};
and if I now for example want to solve Xcell{3,2} with its corresponding Ycell{1,2} I can call the function:
a = myfun(3,2);
And everything works fantastic. However, my next step is to try and do this for all of the indices in the cell array using arrayfun.
But when I write it like this:
a = arrayfun(@(sample,folds) myfun(sample,folds),1:25,1:9,'UniformOutput',0);
I get:
Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 25 in dimension 2. Input #3 has size 9
Could someone who understands arrayfun better than I do please explain what I’m doing wrong and how I should write it to do what I want? Thanks!

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Apr 2016
It's better to use a for loop
  3 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 15 Apr 2016
Ok, then post an example, not necessary matrices of sizes 3600x664. Just a small example
Peta
Peta on 16 Apr 2016
Ok here is an example of how to generate the data for something similar but smaller in dimension to what I’m trying to do:
%%1. Create random data with similar dimensions as before but smaller
for folds = 9: -1 : 1
Ycell{1,folds} = rand(360,1);
for sample = 15 : -1 : 1
Xcell{sample,folds} = rand(360,randi([10 150]));
end
end
%%2. Define function
myfun = @(sample,folds) Xcell{sample,folds}\Ycell{1,folds};
%%3. Try to solve with arrayfun but fail:
a = arrayfun(@(sample,folds) myfun(sample,folds),1:15,1:9,'UniformOutput',0);

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!