Matching columns of arrays to fill up a corresponding empty column
3 次查看(过去 30 天)
显示 更早的评论
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?
Thanks in advance.
Best, Antonios
回答(1 个)
Voss
2022-5-13
load('OCV_new.mat')
load('SoC_internal_new.mat')
load('SOC.mat')
It looks like SoC_internal_new is not values between 0 and 100 percent but between 0 and 1:
min(SoC_internal_new)
max(SoC_internal_new)
And SOC is in percent:
min(SOC)
max(SOC)
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(:)]
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)]
% 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 个评论
Voss
2022-5-17
编辑:Voss
2022-5-17
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):
Data = load('SOC');
load('OCV_new.mat')
load('SoC_internal_new.mat')
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
tic
soc_voltage_data_test = [Data.SOC ...
interp1(100*SoC_internal_new(:),OCV_new(:),Data.SOC,'nearest')];
toc
% both ways give the same result
isequal(soc_voltage_data_test,soc_voltage_data)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parameterization Methods 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!