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

조회 수: 1 (최근 30일)
Peta
Peta 2016년 4월 15일
댓글: Peta 2016년 4월 16일
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!

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 15일
It's better to use a for loop
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 15일
Ok, then post an example, not necessary matrices of sizes 3600x664. Just a small example
Peta
Peta 2016년 4월 16일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by