Borrar filtros
Borrar filtros

How to use if/else on the basis of rows?

3 visualizaciones (últimos 30 días)
Armando MAROZZI el 6 de Sept. de 2020
Comentada: Armando MAROZZI el 6 de Sept. de 2020
I have a pre-specified model that runs smoothly. The problem is that I have two datasets for the same model. The two datasets change just on the basis of the rows:e.g. dataset 1 goes from 1 to 60 and dataset 2 from 61 to 115. All the variables are the same. I would like to avoid computing the same code twice. I'd rather write it nicely and compute it in one shot.
I will give you an example dataset with my model:
data = rand(115,5)
Y_data = data(1:60, :) % dataset 1
Y_data = data(61:115, :) % dataset 2
% This is the model that runs nicely on dataset Y_data. I wanted to avoid to run the model twice,
% first with Y_data from row 1 to 60 and then from row to 61 to 100. I would like to do it in one shot
% the code for the model is fully automated so it's just a matter of making it work first on dataset 1 and then
% on dataset 2 in one unique code
T = size(Y_data,1);
P = 3; % number of lags used in LP for controls
H_min = 1;
H_max = 25;
y = Y_data(:,1); % endogenous variable
x = Y_data(:,2); % shock
w = lagmatrix(Y_data(:,[3:5]), 1:P );
newData = cat(2, y, x, w)
% Remove missings from data
newData(any(isnan(newData), 2), :) = [];
% Re-declare variables after removing missings
y = newData(:,1); % endogenous variable
x = newData(:,2); % shock
w = newData(:,3:size(newData,2)); % control variables and lags
r = 3;
lambda = 10000;
slp = locproj(y,x,w,H_min,H_max,'smooth',r,lambda);
%% Cross-Validation Choice of Lambda
slp = locproj(y,x,w,H_min,H_max,'smooth',r,0.01);
lambda = [1:10:1000] * T;
slp = locproj_cv(slp,5,lambda);
lambda_opt = lambda( min( slp.rss ) == slp.rss );
%% Confidence Intervals
r = 3;
slp = locproj(y,x,w,H_min,H_max,'smooth',r,lambda_opt);
slp = locproj_conf(slp,H_max,lambda_opt/2);
What I thought it could sort this out was using if/else, something like:
% This is wrong but it gives you an idea of what I was trying to do and get
% trying to tell MATLAB, fun the code first from dataset 1 (row 1:60) and then the same on dataset 2 (from row 61:115)
if Y_data = data(1:60, :);
Y_data = data(61:115, :);
% model code as above just here - not to make it too long
% the output therefore should save both results for dataset1 and dataset2
I got stuck and don't manage to go ahead. Can anyone help me? It would make my day.
Thank you very much!
  2 comentarios
Mohammad Sami
Mohammad Sami el 6 de Sept. de 2020
Editada: Mohammad Sami el 6 de Sept. de 2020
Write your code as function and then you can call it with any data you want.
if true
Out1 = myfunction(ydata(1:60,:));
Out2 = myfunction(ydata(61:end,:));
Armando MAROZZI el 6 de Sept. de 2020
Thanks a lot! Super helpful!

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 6 de Sept. de 2020
for k=1:2:3
%everything else the same

Más respuestas (0)


Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by