# Matching columns of arrays to fill up a corresponding empty column

5 views (last 30 days)
Antonios Kouzelis on 10 May 2022
Edited: Voss on 17 May 2022
Hi,
While determining the battery voltage values from battery state of charge values, I'm encountering the following issue:
I have one array of 10.001 rows and 2 colums (10.001x2), giving percentage in steps of 0.01 percent
and one array of 604.800 rows and 2 columns (604.800x2), giving values of the voltage.
The first array describes the relation between state of charge and the voltage potential in the battery. The left column goes from 0 to 100 percent, in steps of 0.01 percent, and the right column gives corresponding voltage values that range from 2.8015 V to 4.1458 V.
0.00 2.8015
0.01 2.8015
0.02 2.8015
... ...
99.98 4.1444
99.99 4.1548
100.00 4.1548
My second array, in the first column, has 604,800 rows of percentage values (one week of state of charge values). The left column starts at 90%, goes up and down a few times, ending up close to 90% again. The second column of this array is empty.
90.00 [empty]
90.00 [empty]
90.00 [empty]
... ...
89.99 [empty]
89.99 [empty]
89.99 [empty]
How can I fill up the second column of the second array ([empty] values), with the values that correspond to those percentages from the first array?
Best, Antonios
Antonios Kouzelis on 12 May 2022
Dear ImageAnalyst,
You were able to be of excellent help last time I posted a question on the forum.
Would you be able to help me once more?

Voss on 13 May 2022
It looks like SoC_internal_new is not values between 0 and 100 percent but between 0 and 1:
min(SoC_internal_new)
ans = 0
max(SoC_internal_new)
ans = 1
And SOC is in percent:
min(SOC)
ans = 20.7939
max(SOC)
ans = 90
So one of them must be converted in order to compare them. I'll convert SoC_internal_new to percent and construct the first array described in the question:
input_data = [100*SoC_internal_new(:) OCV_new(:)]
input_data = 10001×2
0 2.8015 0.0100 2.8015 0.0200 2.8015 0.0300 2.8015 0.0400 2.8015 0.0500 2.8015 0.0600 2.8015 0.0700 2.8015 0.0800 2.8015 0.0900 2.8015
Then the second array described in the question has SOC in the first column and interpolated values in the second column:
output_data = [SOC(:) interp1(input_data(:,1),input_data(:,2),SOC)]
output_data = 604800×2
90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878 90.0000 4.0878
% plot both arrays
plot(input_data(:,1),input_data(:,2))
hold on
% SOC (i.e., output_data(:,1)) is unsorted (i.e.,
% it goes up and down, as described in the question),
% so sort output_data according to the values of
% output_data(:,1), for plotting
[~,idx] = sort(output_data(:,1));
plot(output_data(idx,1),output_data(idx,2),'--','LineWidth',2) ##### 2 CommentsShowHide 1 older comment
Voss on 17 May 2022
You're welcome!
Note that my solution and yours are different, in that I'm doing linear interpolation and you're doing nearest-neighbor (minimum absolute difference) lookup. I don't know if that matters in this case, since the question didn't say which method to use and the data is finely enough spaced that you can't see any difference between the results from the two methods. I just figured I'd point that out in case it matters for your purpose.
However, if you want to use nearest-neighbor, you can do it with the interp1 function much faster than with a for loop (~1000x faster in this case):
tic
n = numel(Data.SOC);
soc_voltage_data = zeros(n,2);
for i =1:n
temp1 = Data.SOC(i,1);
temp2 = SoC_internal_new.'*100;
temp3 = temp2 - temp1;
abs_error = abs(temp3);
% ind_min = find(temp3==0);
[~,ind_min] = min(abs_error);
voltage = OCV_new(1,ind_min);
if numel(voltage) == 0
break
end
soc_voltage_data(i,1) = temp1;
soc_voltage_data(i,2) = voltage;
end
toc
Elapsed time is 13.790107 seconds.
tic
soc_voltage_data_test = [Data.SOC ...
interp1(100*SoC_internal_new(:),OCV_new(:),Data.SOC,'nearest')];
toc
Elapsed time is 0.015937 seconds.
% both ways give the same result
isequal(soc_voltage_data_test,soc_voltage_data)
ans = logical
1

R2022a

### Community Treasure Hunt

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

Start Hunting!