How to calculate the average with "for loop"

Hello. I'm beginner at Matlab.
I want to calculate the average of DTW distances using rows.
For example, I have two datasets, data11(19200 x 25), data22(19200 x 25).
After computing the DTW distance between two rows, I wanna get the average value of all dtw distances.
Please let me know how to get the average value using "For loop" code.
clear all
data11 = csvread('person01_boxing_d1_uncomp.csv');
data22 = csvread('person01_handclapping_d1_uncomp.csv');
x = data11(1,:); % 1 ~ 19200
y = data22(19199,:); % 1 ~ 19200
dtw(x,y);
% I get one dtw distance.
% I want to compute the average value of all kk, kk1, kk3, .....
x = data11(1,:);
y = data22(1,:);
kk = dtw(x,y);
x = data11(1,:);
y = data22(2,:);
kk1 = dtw(x,y);
x = data11(1,:);
y = data22(19199,:);
kk3 = dtw(x,y);
average = mean(kk, kk1, kk3) % ??

 Accepted Answer

How to calculate the average with "for loop"
data11 = csvread('person01_boxing_d1_uncomp.csv');
data22 = csvread('person01_handclapping_d1_uncomp.csv');
x = data11(1,:); % 1 ~ 19200
kk=zeros(1,size(data22,1)-1);
for i=1:length(kk)
y = data22(i,:);
kk(i)= dtw(x,y);
end
mean(kk)

6 Comments

Thank you so much!
Could I change also the x vector from 1 to 19119?
And I want to compute all dtw' between x and y.
x = data11(i,:); % instand of x = data11(1,:);
Yes
data11 = csvread('person01_boxing_d1_uncomp.csv');
data22 = csvread('person01_handclapping_d1_uncomp.csv');
kk=zeros(1,size(data22,1)-1);
for i=1:length(kk)
x = data11(i,:);
y = data22(i,:);
kk(i)= dtw(x,y);
end
mean(kk)
In this case it would be like
x = data11(1,:);
y = data22(1,:);
kk = dtw(x,y);
x = data11(2,:);
y = data22(2,:);
kk1 = dtw(x,y);
.......
x = data11(19199,:);
y = data22(19199,:);
kk19199 = dtw(x,y);
Then mean
Thank you so much!
This code will compute the same rows of x and y.
Can I compute all distances?
x = data11(1,:);
y = data22(i,:); % i = 1 ~ 19200
x = data11(2,:);
y = data22(i,:); % i = 1 ~ 19200
x = data11(3,:);
y = data22(i,:); % % i = 1 ~ 19200
..............
x = data11(19200,:);
y = data22(i,:); % % i = 1 ~ 19200
Yes, in such case you may have to use two nested loop.
Hello. How about this code for all rows?
clear all
data11 = csvread('person01_boxing_d1_uncomp.csv');
data22 = csvread('person01_handclapping_d1_uncomp.csv');
kk=zeros(1,size(data22,1)-1);
for i=1:length(kk)
x = data11(i,:);
for j=1:length(kk)
y = data22(j,:);
kk(j)= dtw(x,y);
end
end
mean(kk)
Seem so
data11 = csvread('person01_boxing_d1_uncomp.csv');
data22 = csvread('person01_handclapping_d1_uncomp.csv');
iter=size(data11,1)-1;
kk=zeros(iter,iter);
for i=1:iter
x = data11(i,:);
for j=1:iter
y = data22(j,:);
kk(i,j)=dtw(x,y);
end
end
mean(kk(:))
You may look for avoid loop (if possible), since it takes a long time due to the large number of repetitions. You may check arrayfun, cellfun or other similar questions in the fourmn. More, suggested to avoid clear all

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!