how to use for loop to compute a matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!