how to use for loop to compute a matrix

조회 수: 1 (최근 30일)
Kong
Kong 2020년 5월 5일
댓글: Sindar 2020년 5월 6일
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

채택된 답변

Sindar
Sindar 2020년 5월 6일
편집: Sindar 2020년 5월 6일
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
Sindar
Sindar 2020년 5월 6일
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

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by