Inputting values into a vector using an if statement
显示 更早的评论
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
回答(1 个)
Atsushi Ueno
2021-4-13
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 个评论
Tru Jacobson
2021-4-13
Atsushi Ueno
2021-4-14
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;
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!