Index exceeds matrix dimensions

this line of code gives error. \bn
TS = traintarg(IX); traintarg{1x240, matrix} and IX{1x300, matrix} \bn
while this line does not and works fine \bn
DiS = Distr(IX); Distr{1x300, matrix} and IX{1x300, matrix}
I am not getting why this error occurs. ASAP

 Accepted Answer

Adam
Adam on 29 Mar 2017
Edited: Adam on 29 Mar 2017
You are indexing into an array of 240 elements in the first case, 300 elements in the second case.
You haven't told us what is in IX, but even if we assume it is just integers between 1 and 300 in some arbitrary order this clearly will give an error when you index into the first array.
So I assume IX contains at least 1 value > 240
Please give details of matrix sizes and anything else relevant after (or before) the code though, don't mix them together into some strange pseudo-code, it makes it more difficult to understand what is actually code and what is just you adding information.

1 Comment

TS = traintarg(IX) \bn
traintarg is a matrix of 1x240 of some random numbers. \bn
IX is also a matrix of 1x300 of some random numbers. \bn
Here is a sample code: \bn
for d = d_min : d_max;
[DS, IX] = sort(trainpat(d,:));
TS = traintarg(IX);
DiS = Distr(IX);
lDS = length(DS);
vPos = 0 * TS;
vNeg = vPos;
i = 1;
j = 1;
while i <= lDS
k = 0;
while i + k <= lDS && DS(i) == DS(i+k)
if(TS(i+k) > 0)
vPos(j) = vPos(j) + DiS(i+k);
else
vNeg(j) = vNeg(j) + DiS(i+k);
end
k = k + 1;
end
i = i + k;
j = j + 1;
end
vNeg = vNeg(1:j-1);
vPos = vPos(1:j-1);
Error = zeros(1, j - 1);
InvError = Error;
IPos = vPos;
INeg = vNeg;
for i = 2 : length(IPos)
IPos(i) = IPos(i-1) + vPos(i);
INeg(i) = INeg(i-1) + vNeg(i);
end
Ntot = INeg(end);
Ptot = IPos(end);
for i = 1 : j - 1
Error(i) = IPos(i) + Ntot - INeg(i);
InvError(i) = INeg(i) + Ptot - IPos(i);
end
idx_of_err_min = find(Error == min(Error));
if(length(idx_of_err_min) < 1)
idx_of_err_min = 1;
end
if(length(idx_of_err_min) <1)
idx_of_err_min = idx_of_err_min;
end
idx_of_err_min = idx_of_err_min(1);
idx_of_inv_err_min = find(InvError == min(InvError));
if(length(idx_of_inv_err_min) < 1)
idx_of_inv_err_min = 1;
end
idx_of_inv_err_min = idx_of_inv_err_min(1);
if(Error(idx_of_err_min) < InvError(idx_of_inv_err_min))
T_MIN(1,d) = Error(idx_of_err_min);
T_MIN(2,d) = idx_of_err_min;
T_MIN(3,d) = -1;
else
T_MIN(1,d) = InvError(idx_of_inv_err_min);
T_MIN(2,d) = idx_of_inv_err_min;
T_MIN(3,d) = 1;
end
end

Sign in to comment.

More Answers (0)

Asked:

on 29 Mar 2017

Edited:

on 29 Mar 2017

Community Treasure Hunt

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

Start Hunting!