Help me Understand why my First Array Index is invalid
85 views (last 30 days)
Show older comments
My used matrices:
Qij_n --> (4x4 matrix)
Pi_E --> (4x1 column matrix)
Aj_E --> (4x1 column matrix)
Fij --> (4x4 matrix)
Kij --> (4x4 matrix)
My operation: (Note: This is in a for loop, and i and j are set to equal to one initially. So im not trying to index a 0th element. I don't know what it is.
I made sure to stay within the same column and only change the rows for the 4x1 matrices. hmm....
Qij_n(i,j) = (Pi_E(i,1)*( (Aj_E(j,1) * Fij(i,j) * Kij(i,j)) ) )
The error message:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
And yes, all the matrices are assigned with values.
Edit: I changed the column matrices to Pi_E(i) and Aj_E(j) since thats easier. Now the error message is just:
Array indices must be positive integers or logical values.
3 Comments
Answers (1)
Rik
on 21 Oct 2018
You also have a warning in the lint on this line. The mlint is a very useful tool in debugging, so you should make sure you have no warnings (orange) or errors (red). If the linter gives a warning you should either resolve it, or you can hide the warning by using %#ok. You should only use that if you are completely sure your way is better than an alternative suggested by mlint.
You shouldn't use i or j as counters because of the potential for bugs. It's not wrong, but avoiding them will prevent hard to find bugs.
There is another hint: if you mean elseif, don't use else if. Your tests also overlap. If tij_E(i,j) <= 10 is false, you don't need to test tij_E(i,m)>10 in your else, because that is guaranteed to be true.
Below you will find a suggested improved function. It runs much faster (which may not be an issue now, but might be with future inputs) and avoids some loops. I would also suggest you read the documentation for the size function. The way you used it suggests you think it creates a matrix of that size, while it actually returns the size of the array you provide as input.
tij_E = [8 11 12 14; 11 9 16 14; ...
17 12 7 11; 10 16 14 11];
Ft = [1.25; 1.05; 0.9];
Kij = [0.95 1.02 1.05 0.9; 1.01 0.98 0.92 1.02; ...
0.99 1 0.96 0.94; 0.92 0.99 1.03 1.05];
Pi_E = [350; 225; 500; 425];
Aj_E = [525; 250; 425; 300];
Fij = zeros(size(tij_E));
% % Future zone-to-zone friction factors Fij
% for k = 1:size(tij_E,1)
% for m = 1:size(tij_E,2)
% if tij_E(k,m) <= 10
% Fij(k,m) = Ft(1);
% elseif tij_E(k,m)<=15
% Fij(k,m) = Ft(2);
% else
% Fij(k,m) = Ft(3);
% end
% end
% end
%logical indexing is faster than a loop:
Fij(tij_E> 15)=Ft(3);
Fij(tij_E<=15)=Ft(2);
Fij(tij_E<=10)=Ft(1);
% Estimated future zone-to-zone trips Qij_n
Qij_n = zeros(size(tij_E));
for k = 1:size(tij_E,1)
for m = 1:size(tij_E,2)
Denom=sum(Aj_E'.*Fij(m,:).*Kij(m,:));%transpose to avoid implicit expansion
Qij_n(k,m) = (Pi_E(k)*( (Aj_E(m) * Fij(k,m) * Kij(k,m)) ) ) / Denom;
end
end
1 Comment
Rik
on 30 Oct 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!