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

Réponses (1)

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 commentaires

I am recieving a 1958x1 array instead of the same size of the original vectors. I am guessing that the if statement is a bit funky. Any ideas?
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;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by