error:index out of bounds
2 views (last 30 days)
Show older comments
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
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?
Answers (1)
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!