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 ??

조회 수: 10 (최근 30일)
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
Guillaume 2019년 6월 28일
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);
  댓글 수: 5
Ramesh Bala
Ramesh Bala 2019년 6월 29일
Thank you for the comment.
Is this
%calculate RMSD
RMSD = sqrt(sum((data(2:end, :) - data(1:end-1, :)) .^2, 2) ./ sum(data(1:end-1, :), 2).^2)
Actually this : RMSDD = sqrt ( sum(( b-a).^2) /sum(a.^2) );
It seems the values aren't matching??
Guillaume
Guillaume 2019년 6월 30일
"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)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 6월 29일
sum(diff(data, 1).^2,1)
  댓글 수: 1
Ramesh Bala
Ramesh Bala 2019년 6월 29일
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);
%
% %walter's comments
% % 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;

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by