Info

This question is closed. Reopen it to edit or answer.

??? Index exceeds matrix dimensions.

1 view (last 30 days)
Captain Singh
Captain Singh on 29 Jan 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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.

Answers (1)

Mischa Kim
Mischa Kim on 29 Jan 2014
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 Comment
Mischa Kim
Mischa Kim on 29 Jan 2014
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!