Finding sequence in matrix
显示 更早的评论
I have a matrix and I want find which row contains some sequence. For example:
- A= [8 5 2 3 -1 0 4 -2 5 0 0 0
- | 5 3 4 -2 1 6 -1 -3 0 0 0 0|
- | -1 3 5 2 0 4 2 0 0 0 0 0];|
- and sequence is:
- seq=[4 -2 1];
- Result shoulde be:
- Result=2
- I tried to use xcorr function like this:
- [m n]=size(A);
- for i=1:m
- fi(i)=max((xcorr(A(i,:),seq)));
- end
- [no index]=max(fi);
- result=index
- But it doesnt work in some cases. Thanks for any help.
采纳的回答
更多回答(1 个)
Image Analyst
2013-3-11
Try this:
A= [8 5 2 3 -1 0 4 -2 5 0 0 0
5 3 4 -2 1 6 -1 -3 0 0 0 0
-1 3 5 2 0 4 2 0 0 0 0 0]
template = [4 -2 1]
% Here's how to do it.
out = normxcorr2(template, A)
[row, column] = find(out == 1)
3 个评论
Image Analyst
2013-3-11
Forgot to mention that it requires the Image Processing Toolbox.
Viktor
2013-3-31
Image Analyst
2013-3-31
Funny -- I just copied and pasted and it ran perfectly. Why do you say it doesn't work?
类别
在 帮助中心 和 File Exchange 中查找有关 Correlation and Convolution 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!