Inputting values into a vector using an if statement
Show older comments
Hey there, I am having some troubles with getting a vector out of my code, currently I am only getting a single r value from my code:
Data1.mat is a 1x1 structure with 5 vectors included: A HomeUse, A RawData, a Time stamp, an atmp vector and a srad vector.
%%
clc
clear all
%%
data=load('Data1.mat');
A=40; %m^2
PR=.86; % no unit
r_baseline=.15; % percent
Batter_Capacity=14; % kWh
Price_Elec=0.16; % $0.16/kWh
Tot_Sys_Cost=20000; % $$$$$$$$$$
Time=data.Time;
HomeUse=data.HomeUse;
atmp=data.atmp;
srad=data.srad;
r=reshape(1:length(atmp),[26304,1]);
%%
for i=1:length(atmp)
if atmp(i)>25
r=.15-(atmp(i)-25).*0.0038;
else
r=r;
end
end
Answers (1)
Atsushi Ueno
on 13 Apr 2021
I think this is what you would like to do.
% for i=1:length(atmp)
% if atmp(i)>25
% r=.15-(atmp(i)-25).*0.0038;
% else
% r=r;
% end
% end
r=.15-(atmp(atmp>25)-25).*0.0038;
2 Comments
Tru Jacobson
on 13 Apr 2021
Atsushi Ueno
on 14 Apr 2021
Do you mean "when atmp = [24, 26, 10, 30, 5], r shall be [1; 0.1462; 3; 0.1310; 5]" ?
atmp = rand(1958,1) * 50; % temporary data for example
% r = reshape(1:length(atmp),[1958,1]); % use transpose instead of reshape
r = (1:length(atmp))'; % see https://jp.mathworks.com/help/matlab/ref/transpose.html
%%
for i=1:length(atmp)
if atmp(i)>25
r(i) = .15-(atmp(i)-25).*0.0038; % you mean when atmp>25 r is calculated
else
r(i) = i; % r=r; you mean when atmp<=25 r is just an index number
end
end
The code above can be reduced like the code below.
atmp = rand(1958,1) * 50; % temporary data for example
r = (1:length(atmp))';
r(atmp>25)=.15-(atmp(atmp>25)-25).*0.0038;
Categories
Find more on Loops and Conditional Statements 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!