Index exceeds the number of array elements (10)

var=cell(CC.NumObjects,1)
for j=1:1:n
% clear var
var(j,1)= var(double(image(CC.PixelIdxList{1,j})))
end
%
my code is like this. CC.NumObjects=10 and for example its consists of CC.PixelIdxList{1,1} = 369x1 array. Others consist of arrays like this. "CC.Pixel dxList{1,1}" values in this are pixel indices. I'm trying to measure the variance of the values corresponding to these indices in the image matrix. but it gives this error.

6 Comments

The error is clear - You are trying to access an index for which data does not exist.
It's not clear what you are trying to do with this line of code.
var(j,1)= var(double(image(CC.PixelIdxList{1,j})))
You pre-allocated var as a cell array and then you are trying to use it as a variable? Makes no sense.
Without more information and the data you are working with, it is difficult to suggest how to proceed. It would be better if you could provide those and also, mention the full error you got i.e. all of the red text.
var(j,1)= var(double(image(CC.PixelIdxList{1,j})))
I am trying to collect the variance values in a table with this row.
image(CC.PixelIdxList{1,j}) This function could be like this image(52) or image(100) or image(150) . 52 and 100 values are array indices. For example:
CC.PixelIdxList{1,7}
result
ans =
26661
26662
26917
26918
26919
27174
27175
27176
27431
27432
27433
27688
27689
27690
27945
27946
27947
28202
28203
28204
28459
28460
28461
28716
28717
28718
28973
28974
28975
29230
29231
29232
29487
29488
29489
29744
29745
29746
30001
30002
30003
30258
30259
30260
30515
30516
30517
30772
30773
30774
I should point out here that the image consists of a 255x237 matrix. So there are 255x237=60435 indeces. Here I take the indexes of certain regions and extract their values in the image matrix, like this:
GPR(CC.PixelIdxList{1,7})
ans =
50×1 uint8 column vector
117
117
116
118
117
116
117
117
116
117
118
116
118
117
117
117
117
117
118
117
117
117
117
116
117
117
116
117
116
117
117
117
117
117
117
117
117
117
116
117
116
116
117
116
116
117
116
116
116
116
Here, the equivalent of 26661 index is given as 117. others in the same way. I want to find the variance of these values. When I remove this code from the for loop and run it for the first time, it does not give an error, but the second time I run it, I get this error.
Index exceeds the number of array elements (10).
Error in imageContour (line 26)
var(j,1)= var(double(GPR(CC.PixelIdxList{1,j})))
I guess you are trying to do this -
var = cell(CC.NumObjects,1);
for j = 1:n
var{j,1} = double(image(CC.PixelIdxList{1,j})));
end
There is no variance here because there is no code. When I write the code, it gives the same error again. So like this
var = cell(CC.NumObjects,1);
for j = 1:n
var{j,1} = var(double(image(CC.PixelIdxList{1,j})));
end
Attach the data you are working with. Use the paperclip button to do so.
Once again, why are you trying to use an empty cell array as a variable as and then over-writing it?
var{j,1} = var(double(image(CC.PixelIdxList{1,j})));
Edit - I seemed to have forgotten that var is a built-in MATLAB function :')

Sign in to comment.

 Accepted Answer

Avoid naming your variable var, since that is the name of a function you are trying to use. Also, a cell array doesn't appear to be required; you can use a numeric vector to store the results.
var_result = zeros(n,1);
for j = 1:n
var_result(j,1) = var(double(GPR(CC.PixelIdxList{1,j})));
end

2 Comments

Thank you, the error has been resolved. I changed the variable name from "var" to "var_result" and it did not give an error.
You're welcome!

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!