MATLAB Answers

how to use for loop to compute a matrix

2 views (last 30 days)
Kong
Kong on 5 May 2020
Commented: Sindar on 6 May 2020
Hello. I'm a beginner at Matlab.
I want to use for loop.
I got data (25 x 12852) using this code.
I want to concatenate the matrix (dist).
For example, the last part of whole code,
dist = dtw(data(:,1),data(:,2));
dist = dtw(data(:,2),data(:,3));
dist = dtw(data(:,3),data(:,4));
dist = dtw(data(:,4),data(:,5));
dist = dtw(data(:,5),data(:,6));
......
dist = dtw(data(:,12851),data(:,12852));
Finally, I want to get one matrix.
Can I get an idea to solve this?
Also, is it resonalble to use dynamic time warping (DTW) for video classification?
clear all
close all
%// read the video:
list = dir('*.avi')
% loop through the filenames in the list
for k = 1:length(list)
reader = VideoReader(list(k).name);
vid = {};
while hasFrame(reader)
vid{end+1} = readFrame(reader);
end
%// estimate foreground as deviation from estimated background:
cellSize = [8 8];
for i=1:25
fIdx(i) = i; %// do it for frame 1 ~ 60
frameGray{i} = vid{fIdx(i)};
[featureVector{i},hogVisualization{i}] = extractHOGFeatures(frameGray{i},'CellSize',cellSize);
end
data = cell2mat(featureVector');
data = double(data);
dist = dtw(data(:,5),data(:,6));
end

  0 Comments

Sign in to comment.

Accepted Answer

Sindar
Sindar on 6 May 2020
Edited: Sindar on 6 May 2020
Assuming each one produces a 25 x 12852 dist array, you can do this fairly easily. Which dimension do you want to concatenate on?
This solution adds each one as a new "page" (3rd dimension)
clear all
close all
%// read the video:
list = dir('*.avi')
% preallocate dist
dist = zeros(25,12852,length(list));
% loop through the filenames in the list
for k = 1:length(list)
...
dist(:,:,k) = dtw(data(:,5),data(:,6));
end
If you want to concatenate on rows:
dist = zeros(25*length(list),12852);
...
dist((1:25)+25*(k-1),:) = dtw(data(:,5),data(:,6));
or columns:
dist = zeros(25,12852*length(list));
...
dist(:,(1:12852)+12852*(k-1)) = dtw(data(:,5),data(:,6));

  1 Comment

Sindar
Sindar on 6 May 2020
Looking back at your code, it seems dist might be a different size, and you may want a second loop over the size of data. Hopefully my answer still helps

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!