Error using + Matrix dimensions must agree.

1 次查看(过去 30 天)
This function implements Boyer moore string matching algorithm. It gives me the following error message at line 19 (start_pos = start_pos + lookup_table(last_Char));
Both start_pos and the index i am looking in lookup_table is 1 * 1 based so i dont get where matrix size mismatch? Can anyone help me with this ??
Error using +
Matrix dimensions must agree.
Error in boyer (line 19)
start_pos = start_pos +
lookup_table(last_char);
function [final_count] = boyer(haystack, needle, nlength, start_pos, end_pos )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
temp_count = 0;
final_count = 0;
offset = nlength;
lookup_table = createTable(needle, nlength);
while(start_pos <= end_pos - nlength + 1)
last_char = haystack(start_pos + nlength - 1);
if(last_char == needle(nlength) & strncmpi(haystack(1,start_pos:end_pos), needle, nlength - 1) == 1)
temp_count = temp_count + 1;
end
start_pos = start_pos + lookup_table(last_char);
end
final_count = final_count + temp_count;
end
  5 个评论
dpb
dpb 2014-4-25
Need to format your code so it's legible...simplest I've found is to just type two spaces at the beginning of a line starting a new paragraph. Keep at it until it looks right in the preview window.
Probably the easiest way for you to find the problem is to use the debugger and when you reach the error point and stop look at the size() of the various terms to see who's the actual culprit. One thought I can't tell easily w/ the unformatted code and little context is is it possible lookup_table ever returns []?
the cyclist
the cyclist 2014-4-25
Ideally, give us the smallest possible self-contained code that we can run. This makes debugging much faster for us. It is also quite common to discover the error yourself, in the process of making that small amount of code.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by