Clear Filters
Clear Filters

Calculating the mean per row of a nested cell arrays and/or RMSE of cell arrays

3 views (last 30 days)
I have a cell array:
A = 3×1 cell array
[14×122 double]
[14×395 double]
[14×424 double]
And cell:
B = 3×1 cell array
[14×122 double]
[14×395 double]
[14×424 double]
I need to calculate the RMSE of the first “within” row of A and the odd “within” rows of B. I am using the following equation:
RMSE = sqrt(mean((y - yhat).^2));
I have working:
cellfun(@(p) power(p,2),(cellfun(@(x,y) x(1,:) - y(1:2:end,:), test_Targets, Predicted_Targets,'uni', false)),'uni',false);
Which gives me the squared values of the difference of the rows I need. This returns a
ans = 3×1 cell array
[7×122 double]
[7×395 double]
[7×424 double]
Then, I am stuck at trying to calculate the mean. Technically, I need to do mean(C{i}(j,:)) but I was not have to implement it. I would like to have just one line for the whole RMSE. I tried
C=cellfun(@(x) power(x,2),(cellfun(@(x,y) x(1,:) - y(1:2:end,:), test_Targets, Predicted_Targets,'uni', false)),'uni',false);
j =cellfun(@(x) transpose(1:size(x,1)/2), Predicted_Targets,'uni',false);
cellfun(@(v,w) mean(v(w,:)), C,j,'uni',false)
ans = 3×1 cell array
[1×122 double]
[1×395 double]
[1×424 double]
Which is wrong because I am expecting a 3x1 containing 7x1 values.
At the end I would like to have something like:
RMSE = sqrt(arrayfun(@mean,cellfun(@(x) power(x,2),(cellfun(@(x,y) x(1,:) - y(1:2:end,:), test_Targets, Predicted_Targets,'uni', false)),'uni',false),'uni', false));
(This code is also wrong. It was my initial try before trying to implement the j. I also tried immse since it was suggested in another answer).
Thank you
  1 Comment
dpb
dpb on 30 Jun 2018
OK, so what is the actual computation you're trying to do; I got lost in the missteps along the way. I think if you illustrate with small subset the calculation and show the result for the sample data and how to get it (mathematically, not necessarily by code) folks will have better chance to write a solution.

Sign in to comment.

Accepted Answer

ErikaZ
ErikaZ on 5 Jul 2018
I had to use 2 for loops, I couldn't figure it out with cellfun and/or arrayfun.
% Individual RMSE for angle and moment
C=cellfun(@(x) power(x,2),(cellfun(@(x,y) x(1,:) - y(1:2:end,:), test_Targets, Predicted_Targets,'uni', false)),'uni',false);
B=cellfun(@(x) power(x,2),(cellfun(@(x,y) x(2,:) - y(2:2:end,:), test_Targets, Predicted_Targets,'uni', false)),'uni',false);
for i=1:3
for j=1:7
RMSE_angle{i,1}(j,1)=sqrt(mean(C{i,1}(j,:)));
RMSE_moment{i,1}(j,1)=sqrt(mean(B{i,1}(j,:)));
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!