Info

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

Error message "Index exceeds matrix dimensions"

1 view (last 30 days)
Simon Emery
Simon Emery on 21 May 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, i got the same issue.
I have 3 matrix. the first one is for data vector in matrix [1 2 3 12 11 6 7 8 9 10], the second is for the ii [1 2 3 3 3 7 7 8 9 10] and the last matrix is for the jj (column) [10 2 3 6 8 1 7 3 9 6]
when i creat my function and launch a test with this approach it is work properly... well at least... SOMEONE COULD HELP ME?
afficheMatriceCreuse([1 2 3 12 11 6 7 8 9 10], [1 2 3 3 3 7 7 8 9 10],[10 2 3 6 8 1 7 3 9 6])
i got this error message:
(1,10) 1
(2,2) 2
(3,3) 3
(3,6) 12
(3,8) 11
(7,1) 6
(7,7) 7
(8,3) 8
(9,9) 9
(10,6) 10
Index exceeds matrix dimensions.
Error in afficheMatriceCreuse (line 31)
fprintf('(%i,%i) %i \n', iM(ii),jM(ii),vM(ii))
or Index exceeds matrix dimensions.
Error in afficheMatriceCreuse (line 26)
iM(ii);
Function:
function afficheMatriceCreuse(vM,iM,jM)
for ii = 1:(numel(iM,jM,vM))
(line26) iM(ii);
jM(ii);
vM(ii);
(line31) fprintf('(%i,%i) %i \n', iM(ii),jM(ii),vM(ii))
end
end

Answers (2)

Alex Mcaulley
Alex Mcaulley on 22 May 2019
Edited: Alex Mcaulley on 22 May 2019
Your for loop goes from 1 to the sum of the elements of the three inputs (in your example, 30), then, when ii = 11:
iM(ii);
jM(ii);
vM(ii);
throws an error, because the size of that arrays is 10x1. I don't know what you are trying to do, but if your inputs are always of the same size, probably yo need to change the limits of the loop to:
for ii = 1:numel(iM)
  1 Comment
Simon Emery
Simon Emery on 22 May 2019
Hi Alex,
I find my issue yesterday, now it's works properly.
function afficheMatriceCreuse(vM,iM,jM)
for ii = 1:(numel(iM,jM))
for jj = 1: numel(vM)
iM(ii);
jM(ii);
vM(ii);
end
fprintf('( %i, %i) %i \n', iM(ii),jM(ii),vM(ii))
end
end

Image Analyst
Image Analyst on 23 May 2019
How about:
% Find out how many rows we should print out, allowing for different sized matrices:
maxNumberOfRows = min(size(iM, 1), size(jM, 1), size(vM, 1)); % Find out the shortest matrix.
% Print header line.
fprintf('(iM, jM) vM\n');
% Print as many rows as we can.
for row = 1 : maxNumberOfRows
fprintf('( %d, %d) %d \n', iM(row), jM(row), vM(row));
end

Community Treasure Hunt

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

Start Hunting!