# I'm trying to find the RMSD of the numbers by taking 1-2,2-3,3-4 in a sequence ,how can i loop as such ??

9 views (last 30 days)
Ramesh Bala on 28 Jun 2019
Commented: Guillaume on 30 Jun 2019
I'm right now doing as such,how to form a loop by taking in the previous nos?
N = [ 55378 55344 55310 55276 55242 55208];
a = N(1,1);
b = N(1,2);
RMSDD1 = sqrt ( sum(( b-a).^2 /sum(a).^2) );
fprintf ('the RMSD value between two nos : %f\n' ,RMSDD1);
RMSDDP = RMSDD1*100;
fprintf ('the RMSD value between two nos in % : %f\n' ,RMSDDP);
a = N(1,2);
b = N(1,3);
RMSDD2 = sqrt ( sum(( b-a).^2 /sum(a).^2) );
fprintf ('the RMSD value between two nos : %f\n' ,RMSDD2);
RMSDDP = RMSDD2*100;
fprintf ('the RMSD value between two nos in % : %f\n' ,RMSDDP);

Guillaume on 28 Jun 2019
No idea what RMSD is. In your formula,
RMSD = sqrt(sum(b-a).^2 / sum(a).^2);
none of the sum make any sense, since both a and b are scalar. sum has only one number to sum, so it is equivalent to:
RMSD = sqrt((b-a)^2 / a^2); %what was the point of the sums?
Assuming that it is the correct formula, to apply it to each element of your vector it would be:
N = [55378, 55344, 55310, 55276, 55242, 55208];
RMSD = sqrt((N(2:end) - N(1:end-1)).^ 2 ./ N(1:end-1).^2);
Guillaume on 30 Jun 2019
"Comments from guilame gives me : 833,1341,1711 as RMSD results ? But the actual results shld be 19,11,5."
My code gives you exactly the same output as the one you've posted, except that it is a lot more generic (will work with any number of files) and simpler.
Output from the code you've given above:
the RMSD value between two sensor signals 1,2 @ 5,200 kHz : 833.775403
the RMSD value between two sensor signals DC @ 5,200 kHz : 83377.540327
the RMSD value between two sensor signals 1,2 @ 5,200 kHz : 1341.762683
the RMSD value between two sensor signals DC @ 5,200 kHz : 134176.268293
the RMSD value between two sensor signals 1,2 @ 5,200 kHz : 1717.228148
the RMSD value between two sensor signals DC @ 5,200 kHz : 171722.814834
You get 833, 1341 and 1717 as well.
Of course, if the formula in your code was wrong, then the formula in mine is as well. I've just implemented exactly the same in more efficient way.
As Walter pointed out, it can be implemented a bit simpler as well, by using diff.
filenumbers = [18502, 22172, 24677, 25892];
%load data as numel(filenumbers) x N matrix
filecontents = arrayfun(@(fn) load(sprintf('%d.mat', fn)), filenumbers); %this will error if the files don't all have exactly the same variable names
data = vertcat(filecontents.ans); %this will error if the files don't have an ans variable or if the ans vectors are not the same length
%calculate RMSD
RMSD = sqrt(sum(diff(data, 1) .^2, 2) ./ sum(data(1:end-1, :), 2).^2)

### More Answers (1)

Walter Roberson on 29 Jun 2019
sum(diff(data, 1).^2,1)
##### 1 CommentShowHide None
Ramesh Bala on 29 Jun 2019
Alright still I get varying results ??
Comments from guilame gives me : 833,1341,1711 as RMSD results ?
Comment from Walter gives me : 1*1024 doubles ?
But the actual results shld be 19,11,5.
N = [18502, 22172, 24677, 25892]; % data
% filecontents = arrayfun(@(fn) load(sprintf('%d.mat', fn)), N); %this will error if the files don't all have exactly the same variable names
% data = vertcat(filecontents.ans); %this will error if the files don't have an ans variable or if the ans vectors are not the same length
% %calculate RMSD - guillame comments
% RMSD = sqrt(sum((data(2:end, :) - data(1:end-1, :)) .^2, 2) ./ sum(data(1:end-1, :), 2).^2);
%
% % RMSD = sum(diff(data, 1).^2,1) % gives a 1024 double results ? instead of a scalar
a = (N(1));
b = (N(2));
RMSDD1 = sqrt ( sum(( b-a).^2) /sum(a.^2) );
RMSDDP1 = RMSDD1*100;
a = (N(2));
b = (N(3));
RMSDD2 = sqrt ( sum(( b-a).^2) /sum(a.^2) );
RMSDDP2 = RMSDD2*100;
a = (N(3));
b = (N(4));
RMSDD3 = sqrt ( sum(( b-a).^2) /sum(a.^2) );
RMSDDP3 = RMSDD3*100;