Index exceeds the number of array elements (Very odd case)

1 view (last 30 days)
In a task,
  1. 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).
  2. I obtain the unique values of the variables soc and ocv as SOCu (432x1) and OCVu (281x1), along with their respective indices as shown.
  3. 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.
  4. 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:
  1. I generated a sequence of random numbers rand_seq (1,130)
  2. I generate a vector to imitate a set of indices, ind (1,37)
  3. 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.
  4. WHY DOES THIS NOT WORK IN THE ABOVE CASE? WHAT AM I MISSING? Kindly provide suggestions.
  5. 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

Answers (1)

dpb
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
Abeer Chaudhry
Abeer Chaudhry on 11 Dec 2020
Thank you for your help. I kind of understand the problem.
Though what I'm trying to achieve here is that for all the unique values of ocv, that is , OCVu, I am aimnig to get the values of SOCu that correspond to that index of OCVu. These two variables are dependent. Once I have the corresponding values, I will interpolate to get another variable.
What can be an alternative to this? How can this problem be taken care of?
dpb
dpb on 11 Dec 2020
Be easier if you could show a small example of the data and what you're intending/wanting.

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!