Info

此问题已关闭。 请重新打开它进行编辑或回答。

??? Index exceeds matrix dimensions.

1 次查看(过去 30 天)
Captain Singh
Captain Singh 2014-1-29
关闭: MATLAB Answer Bot 2021-8-20
Hello everyone, I have given here my matlab program: ______________________________________________________________________________________________
function [ha hb] = func_get_h2(Elxoctet, Vlxoctet, q, rho)
abs_El0octet = abs(Elxoctet(2:end)); diff_E0 = abs(abs_El0octet - q);
% add +1 to compensate for the fact that we remove index 1 from Elxoctet diff_E0 = logical(diff_E0); index_E0 = find(diff_E0 == min(diff_E0)) + 1;
err_E0 = abs_El0octet(index_E0-1) - q; diff_E = abs_El0octet(index_E0) - q;
if (diff_E < 0) index2_E0 = index_E0 - 1; else index2_E0 = index_E0 + 1; end
if( (err_E0 > 0.0001) && (index_E0 < 2) ) h = zeros(length(rho),1); ha = zeros(length(rho),1); hb = zeros(length(rho),1); else ha = Vlxoctet(:,index_E0); hb = Vlxoctet(:,index2_E0); end
% if(err_E0 < 0.0001) % ha = Vlxoctet(:,index_E0); % hb = Vlxoctet(:,index2_E0); % else % ha = zeros(length(rho),1); % hb = zeros(length(rho),1); % end end _______________________________________________________________________________________________ when I executed the file, I got the error: ------------------------------------------------------------------------------------------------ ??? Index exceeds matrix dimensions.
Error in ==> func_get_h2 at 12 diff_E = abs_El0octet(index_E0) - q; ------------------------------------------------------------------------------------------------- Please help me to solve this problem and try to find out the possible error source. Thanks a lot to giving me your precious time. Thank you all of you.

回答(1 个)

Mischa Kim
Mischa Kim 2014-1-29
Captain, it's hard to say based on the code you provide. In the command line
diff_E = abs_El0octet(index_E0) - q;
you are trying to access an element of array abs_El0octet that does not exist. E.g., this is like trying to access the 10th element of a vector that has only a total of 9 elements.
  1 个评论
Mischa Kim
Mischa Kim 2014-1-29
Just a thought. If the last element of diff_EO is the smallest one
index_E0 = find(diff_E0 == min(diff_E0)) + 1;
then index_E0 is greater (by one) than the number of elements in the vector(s). And that will cause the program to crash when trying to access that (n+1)st element.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by