Multiply array if number is under certain value
Show older comments
Im using a script to calculate power from two varaiables. Though i need to multiply my power (P_out) with a certain value (for example 2) if the value is under 1.000.000 to correct my data...
So i have 144 numbers, and all numbers beneath 1.000.000 needs to be multiplied by 2. Maybe 30 of these values are beneath 1.000.000...
How do i do this? Hope someone knows this
This is the code:
Data = readtable('Rejstrup Pyra .xlsx'); %Celcius
T_Pyra = table2array(Data(83:(223),8))
Radiation = table2array(Data(83:(223),3)) %kW/m^2
PV_radiation = Radiation./1000
T_modul = (T_Pyra)+(PV_radiation/20) %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100)
I_korrektion = 0.988
P_peak = 23947000 %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation %Watt
^^^^^^^^
This is my values for my power, where every value beneath 1.000.000 needs to be multiplied with 2.
Exporting it to excel (not relevant):
table_name=table(P_out1)
filnavn='Beregnet P_available fra Rejstrup.xlsx'
overskrift='P_available'
writetable(table_name,filnavn,'Sheet',overskrift)
Best regards
Peter
Answers (1)
Mathieu NOE
on 22 Mar 2022
hello
this is very simple - see below :
P_out = 1000000 + 80000*randn(10,1); % dummy data
plot(P_out)
hold on
ind = P_out<=1000000; % find values below 1000000
P_out(ind) = P_out(ind)*2; % correction (put the right correction factor here)
plot(P_out,'-*')
hold off
29 Comments
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
ok
can you show the updated code and for my info , what are we comparing ? the picture shows 3 vectors (P_out, power , matlab) ... what is the supposed P_out before and after correction in your code ?
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
hello again
the error came because you left my "dummy" data line active in your code
P_out = 1000000 + 80000*randn(10,1); % dummy data
that was of course to be removed
now it's working fine ,

but not yet to the point of being matching the new request for correction between 6:30 and 9:30
Data = readtable('Rejstrup Pyra .xlsx'); %Celcius
T_Pyra = table2array(Data(87:(222),8));
Radiation = table2array(Data(87:(222),3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
plot(P_out)
hold on
ind = P_out<=1000000; % find values below 1000000
P_out(ind) = P_out(ind)*2; % correction (put the right correction factor here)
plot(P_out,'-*')
hold off
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
I was just finishing to modify your code for the correction in amplitude (x2 befoe 9:30 AM)
this is the code (a bit improved) and the result
but this is not what your last picture is telling me . It's a different correction method
so far the code is as pr your initial request

Data = readtable('Rejstrup Pyra .xlsx'); %Celcius
ind_start = 87;
ind_stop = 222;
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(Data.GeneratedOn);
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
plot(Data_time,P_out)
hold on
ind = 116 - ind_start; % find time line corresponding 9:30 AM
P_out(1:ind) = P_out(1:ind)*2; % correction (put the right correction factor here)
plot(Data_time,P_out,'-*')
legend('before correction','after correction');
hold off
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
Please clarify
this is your picture a bit modified by myself (the red line is the extrapolation of the blue line for the first hours) : do you mean this is what you want to achieve ?

but the P_out graph I get even before any correction does not show this initial drop
this is what I get when I start the plot even earlier

Data = readtable('Rejstrup Pyra .xlsx'); %Celcius
ind_start = 87-40;
ind_stop = 222;
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(Data.GeneratedOn);
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
plot(Data_time,P_out)
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
hmmm
this is how your Data table names are defined in my workspace (I run R2020b)
maybe the name of the first columns of Data is different in your matlab. Please correct on your side if you have a different name

Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
Sorry but the correction you want is not a factor 2. it much less than that (and it varies with the time value) but I cannot guess it because the grey reference line is not supplied in your data ...
also I don't understand why the blue should match the grey curve simply for the appearance , you are introducing a bias on purpose in your code just to make things look better ??
In the mean time I fixed the previous problem (Unrecognized table variable name 'GeneratedOn'.)
Data = readtable('Rejstrup Pyra .xlsx','VariableNamingRule' ,'preserve'); %Celcius
ind_start = 87-40;
ind_stop = 222;
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(table2array(Data(:,1)));
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
plot(Data_time,P_out)
Mathieu NOE
on 22 Mar 2022
ok
let's start again from the beginning
where are the data relative to the grey (reference) and your data (blue)
I cannot see anything like the blue line with the initial drop
I wonder if your matlab code is about is the grey curve and not the blue ??
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
okay
it's my turn to become nuts ... :) I still don't get what you want to correct from the P_out generated by the matlab code.
The top plot is your curves , the bottom one is the full 24 hours of P_out as it comes without any further corrections beside what you have already coded
so what correction in the lower plot do you want to do ? that is still unclear to me - make a handheld sketch if you want.

code used for the lower plot :
clc
clearvars
Data = readtable('Rejstrup Pyra .xlsx','VariableNamingRule' ,'preserve'); %Celcius
[m,n] = size(Data);
ind_start = 1; % 87-40
ind_stop = m; % 222
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(table2array(Data(:,1)));
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
plot(Data_time,P_out)
Mathieu NOE
on 22 Mar 2022
seems I get some of your comments much earlier in my outlook vs here in the forum, which make things a bit complicate to follow
Peter Lyngbye
on 22 Mar 2022
Edited: Peter Lyngbye
on 22 Mar 2022
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
ok
this is where you should have started first
I took your last excel file (I removed the columns above B to speed up the code) into consideration
now I can draw the two lines (and understand the purpose of the code)
you can do all modifications you want up to force the blue to match the grey (simply compute the amplitude ratio at each time interval... or build a strategy around that)
so far it seems now you have reached your target...

clc
clearvars
%% reference data (grey)
Data_ref = readtable('Rejstrup Effekt.xlsx','VariableNamingRule' ,'preserve'); %Celcius
[m,n] = size(Data_ref);
ind_start = 60; %
ind_stop = 222; %
Data_ref = Data_ref(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_ref_time = datetime(table2array(Data_ref(:,1)));
P_out_ref= table2array(Data_ref(:,2));
plot(Data_ref_time,P_out_ref)
%% measurement (blue, to be corrected)
Data = readtable('Rejstrup Pyra .xlsx','VariableNamingRule' ,'preserve'); %Celcius
[m,n] = size(Data);
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(table2array(Data(:,1)));
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
% heavy correction : make the blue match exactly the grey
cor_factor = P_out_ref./(P_out+eps); % + eps to avoid division by zero !
P_out_corrected = P_out.*cor_factor;
plot(Data_ref_time,P_out_ref,'k',Data_time,P_out,'b',Data_time,P_out_corrected,'c*')
legend('ref (grey)','blue (uncorrected)','blue (100% corrected)');
Mathieu NOE
on 22 Mar 2022
the size of the Data table , but yes I don't use it in the code (in the last version) so maybe you have a warning but the code runs ok
Peter Lyngbye
on 22 Mar 2022
Mathieu NOE
on 22 Mar 2022
ok
I put back those lines in the code (see below) , but I have a hard time to see what the 0.97 makes as an improvement ?? or did I forgot another piece of code from the past ?
this is not doing any shift in the early hours...
see figure 1 (below)

figure 2 is simply the "heavy" correction method I already posted above

clc
clearvars
%% reference data (grey)
Data_ref = readtable('Rejstrup Effekt.xlsx','VariableNamingRule' ,'preserve'); %Celcius
% [m,n] = size(Data_ref);
ind_start = 60; %
ind_stop = 222; %
Data_ref = Data_ref(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_ref_time = datetime(table2array(Data_ref(:,1)));
P_out_ref= table2array(Data_ref(:,2));
plot(Data_ref_time,P_out_ref)
%% measurement (blue, to be corrected)
Data = readtable('Rejstrup Pyra .xlsx','VariableNamingRule' ,'preserve'); %Celcius
% [m,n] = size(Data);
Data = Data(ind_start:ind_stop,:); % retrict all table data between two time indexes from 7:10 AM to 18:25 PM
Data_time = datetime(table2array(Data(:,1)));
T_Pyra = table2array(Data(:,8));
Radiation = table2array(Data(:,3)); %kW/m^2
PV_radiation = Radiation./1000;
T_modul = (T_Pyra)+(PV_radiation/20); %Celcius
T_korrektion = 1-(T_modul-T_Pyra).*(0.40/100);
I_korrektion = 0.988;
P_peak = 23947000; %watt
P_out = P_peak.*T_korrektion.*I_korrektion.*PV_radiation; %Watt
%% basic correction
P_out_corrected = P_out; % init P_out_corrected
ind = 116 - ind_start; % find time line corresponding 9:30 AM
Data_ref_time(ind)
P_out_corrected(1:ind) = P_out_corrected(1:ind)*0.97; % correction # 1 (put the right correction factor here)
figure(1),
plot(Data_ref_time,P_out_ref,'k',Data_time,P_out,'b',Data_time,P_out_corrected,'c*')
legend('ref (grey)','blue (uncorrected)','blue (corrected)');
%% full correction (remove code below if necessary)
% make the blue match exactly the grey
P_out_corrected = P_out; % init P_out_corrected
ind = P_out<eps;
P_out_ref(ind) = 0; % avoid very high (inf) cor_factor due to division by eps !
cor_factor = P_out_ref./(P_out+eps); % + eps to avoid division by zero if P_out = 0 !
P_out_corrected = P_out_corrected.*cor_factor;
figure(2),
plot(Data_ref_time,P_out_ref,'k',Data_time,P_out,'b',Data_time,P_out_corrected,'c*')
legend('ref (grey)','blue (uncorrected)','blue (corrected)');
Categories
Find more on Classification 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!