Index exceeds the number of array elements (Very odd case)
1 view (last 30 days)
Show older comments
In a task,
- I am reading an excel file and assigning variables a set of values. The size of each of the derived variables is the same (451x1).
- I obtain the unique values of the variables soc and ocv as SOCu (432x1) and OCVu (281x1), along with their respective indices as shown.
- Next, I try to save the values of SOCu that correspond with the values of OCVu using the indices of OCVu as shown in the last line of code.
- This is where it says Index exceeds the number of array elements (432).
I have a the following simple setup:
data = xlsread('1-A pulse charge data.xlsx');
% Just reading data and assigning variables
i=data(8:end,4);
V=data(8:end,3);
soc=data(8:end,5);
ocv=data(8:end,6);
[SOCu, ind1] = unique(soc); % taking unique values
[OCVu, ind2] = unique(ocv);
SOCu = SOCu(ind2); %% this is where the error occurs
On another note, to be able to understand what might be wrong above, I generated a similar example with random numbers:
- I generated a sequence of random numbers rand_seq (1,130)
- I generate a vector to imitate a set of indices, ind (1,37)
- I apply the same step here, as in the last line of the previous code, and it works despite having different dimensions. I get the required/expected output.
- WHY DOES THIS NOT WORK IN THE ABOVE CASE? WHAT AM I MISSING? Kindly provide suggestions.
- The lines are as below
Thank you
rand_seq = randn(1,130) % randomly
ind = round(1+abs(10*rand(1,37))); %A randomly generated set of integers that has a length<< than that of the rand_seq (all
% are positive non-zero integers)
rand_seq = rand_seq(ind) % This conforms
0 Comments
Answers (1)
dpb
on 10 Dec 2020
Edited: dpb
on 11 Dec 2020
Well, yes, that can happen but it won't necessarily always, depending upon what the data are in the two arrays.
...
[SOCu, ind1] = unique(soc); % taking unique values
[OCVu, ind2] = unique(ocv);
SOCu = SOCu(ind2); %% this is where the error occurs
You said above SOCu (432x1) and OCVu (281x1), but that "each of the derived variables is the same (451x1)."
Thus, an element of either ind1 or ind2 can be as large as 451 since it is the index to the location of the first unique value in the array being searched which has numel() = 451. On occasion, the very last element in the array may be unique; in that case that will be the last value of one of the index arrays.
If you then try to use that to address a smaller array, then "BOOM!", yes, Virginia, that is an addressing error.
It's not clear what you think you're doing there; the indexing vectors returned from unique are only valid in reference to the A position argument in the call to unique that generated them.
2 Comments
dpb
on 11 Dec 2020
Be easier if you could show a small example of the data and what you're intending/wanting.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!