shifting without for loop

Hello,
I have two cells each 60x23 and each cell consist of columns with different length.
For each cell, I need
1- Get ten values starting from last values,
2- Apply fitlm function on the obtained values
3- Get the Coefficients {2,1} of the fitlm
4- omit the last value in step1, shift one value and get ten elements. Then repeat steps 2and3. (hopefully without for loops as i found it complicated with cells having variable lengths).
5- Goal: obtain a 60x23 cell where each cell has the Coefficients of linear regression for each ten values.
My attempted solution works till step 3 as shown:
Time2sec = lst2val % 60x23 cell
delta2sec = delta.delta % 60x23 cell
len0 = cellfun(@length, Time2sec, 'uni',0)
len1 = cellfun (@(x) x-9, len0, 'uni',0)
%len1 = cellfun (@(x) x-10, len0,'uni',0)
len2 = cellfun (@(x) x+9, len1, 'uni',0)
last10frames = cellfun (@(x,y1, y2) x( y1:y2 ), Time2sec, len1, len2 , 'Uni',0)
len00 = cellfun(@length, delta2sec, 'uni',0)
len11 = cellfun (@(x) x-9, len00,'uni',0)
len22 = cellfun (@(x) x+9, len11, 'uni',0)
last10frames_2 = cellfun (@(x,y1, y2) x( y1:y2 ), delta2sec, len11, len22 , 'Uni',0)
reg0 = cellfun (@fitlm, last10frames, last10frames2, 'uni',0)
M_value = cellfun (@(x) x.Coefficients{2,1}, reg0 , 'uni', 0)
% omit value, shift with step -1 till first element in each cell and apply linear regression.
I really appreciat the help.

5 Comments

"- omit the last value in step1, shift one value and get ten elements. Then repeat steps 2and3. (hopefully without for loops)."
Even if you write cellfun or arrayfun, ML builds a loop internally; just that you don't see it.
Sometimes, it's just the far simpler way and, owing to the complexity of how to vectorize such expressions and the overhead in the functions themselves, a for loop may actually be faster besides.
Suppose that the Time2sec{1,1} is a vector with 12 values.
What specifically should the output M_value{1,1} be?
I'm guessing a 3x1 vector -- the coefficient for each of three possible regressions on 10 consecutive values:
  • Time2sec{1,1}(1:10)
  • Time2sec{1,1}(2:11)
  • Time2sec{1,1}{3:12)
Is that right?
I agree with @dpb that avoiding the for loop here is probably unnecessary.
Housam
Housam on 22 Jul 2019
Edited: Housam on 22 Jul 2019
yes, you are correct. if it were a vector with 12 values, there should be 3 groups of ten values. @the cyclist
I thought this approach would be less complicated than for loops for cells having different lengths.
For a given cell location (e.g. i=9, j=13), do Time2sec and delta2sec always have the same length?
yes, also correct @the cyclist

Sign in to comment.

 Accepted Answer

the cyclist
the cyclist on 22 Jul 2019
Edited: the cyclist on 22 Jul 2019
If the vectors in the corresponding cell arrays are equal in length, then this straightforward code should work:
M = 60;
N = 23;
MIN_VEC_SIZE = 10;
MAX_VEC_SIZE = 30;
% Make up some data
rng default
Time2sec = cell(M,N);
delta2sec = cell(M,N);
M_value = cell(M,N);
Time2sec = cellfun(@(x)randn(max(MIN_VEC_SIZE,randi(MAX_VEC_SIZE)),1),Time2sec,'uni',false);
delta2sec = cellfun(@(x)randn(size(x)),Time2sec,'uni',false);
% Calculate the regressions
for ii = 1:M
for jj = 1:N
numberRegressionsThisCell = numel(Time2sec{ii,jj})-9;
M_value{ii,jj} = zeros(numberRegressionsThisCell,1);
for nr = 1:numberRegressionsThisCell
mdl = fitlm(Time2sec{ii,jj}(nr:nr+9),delta2sec{ii,jj}(nr:nr+9));
M_value{ii,jj}(nr) = mdl.Coefficients{2,1};
end
end
end
(If not, this code could be easily modified to accommodate that.)
The vast majority of the execution time is spent doing the regressions, so there will likely be little advantage in excising the for loops.

More Answers (0)

Asked:

on 22 Jul 2019

Commented:

on 23 Jul 2019

Community Treasure Hunt

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

Start Hunting!