error:index out of bounds

2 views (last 30 days)
Seprienna
Seprienna on 4 Sep 2014
Commented: Seprienna on 4 Sep 2014
folks, can someone help me to figure out where is the mistake from this simple code. I'm getting error "Attempted to access data_matrix(:,169); index out of bounds because size(data_matrix)=[18,168]."
for f=1:blockSizeR
for g=1:blockSizeC
Ism = X(blockVectorR:blockRR , blockVectorC:blockCC);
data_matrix = [data_matrix,feat_vect];
end
end
ids_train = [];
train_data = [];
cont = 1;
for ii=1:100
for jj=1:6
if jj<2
train_data = [train_data,data_matrix(:,cont)];
ids_train = [ids_train, ids(cont)];
else
test_data = [test_data,data_matrix(:,cont)];
ids_test = [ids_test,ids(cont)];
end
cont = cont + 1;
end
end
  3 Comments
Seprienna
Seprienna on 4 Sep 2014
Edited: Seprienna on 4 Sep 2014
Actually this is the part where the error comes from Any advise?
ids_train = [];
train_data = [];
cont = 1;
for ii=1:100
for jj=1:6
if jj<2
train_data = [train_data,data_matrix(:,cont)];
ids_train = [ids_train, ids(cont)];
else
test_data = [test_data,data_matrix(:,cont)];
ids_test = [ids_test,ids(cont)];
end
cont = cont + 1;
end
end
A Jenkins
A Jenkins on 4 Sep 2014
What are you trying to do? cont is going to increase each time 6 times through the inner loop, then each time 100 times that through the outer loop, so it is going to count up to 601 by the time your loop is done. If your data_matrix is not that big, then what elements would you like to select from it?

Sign in to comment.

Answers (1)

Adam
Adam on 4 Sep 2014
Edited: Adam on 4 Sep 2014
Your 'cont' variable is ranging up to > 600 after 100 outer loops and 6 inner loops so if you only have a 2nd dimension of 168 in your data_matrix then that would be the reason.
Did you perhaps mean to increment cont inside the if statement? i.e.
ids_train = [];
train_data = [];
cont = 1;
for ii=1:100
for jj=1:6
if jj<2
train_data = [train_data,data_matrix(:,cont)];
ids_train = [ids_train, ids(cont)];
else
test_data = [test_data,data_matrix(:,cont)];
ids_test = [ids_test,ids(cont)];
cont = cont + 1;
end
end
end
That would keep 'cont' to a maximum value of 100 though whether that is what you want if you have 168 elements in that 2nd dimension I couldn't say.
  1 Comment
Seprienna
Seprienna on 4 Sep 2014
maybe i didn't explain in details. cont is to calculating the total number which in this case i'm looking at 601. let me attached a fuller code here, if i add-in verbose,it seems to work where cont-1, but i end up getting a different error. "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
I'm not sure which part of the coding i shall change,please advise
if(verbose)
disp(sprintf('Finished assigning image %i/%i to appropriate image set',cont-1,400));
end
the error come from here instead.
results.match_dist = zeros(d,b);
for i=1:d
for j=1:b
results.match_dist(i,j) = return_PhD_distance(train(1:n,j),test(1:n,i),dist,covar);
if train_ids(j)==test_ids(i)
results.same_cli_id(i,j)=1;
else
results.same_cli_id(i,j)=0;
end
end
end

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!