필터 지우기
필터 지우기

How can reduce code?

조회 수: 1 (최근 30일)
FRANCISCO JAVIER
FRANCISCO JAVIER 2021년 5월 31일
편집: Jan 2021년 5월 31일
I want to reduce the following code, where ind_cond is sub-set that satisfy cetain conditions. Someone can help me, please?
for k=1:length(ind_cond)
ofertas_c(k).Fecha = Fecha{ind_cond(k),1};
ofertas_c(k).Contrato = Contrato{ind_cond(k),1}{1,1};
ofertas_c(k).Zona = Zona{ind_cond(k),1};
ofertas_c(k).Agente = Agente{ind_cond(k),1};
ofertas_c(k).Unidad = Unidad{ind_cond(k),1};
ofertas_c(k).Precio = Precio{ind_cond(k),1};
ofertas_c(k).Cantidad = Cantidad{ind_cond(k),1};
ofertas_c(k).Tipo_oferta = Tipo_oferta{ind_cond(k),1};
ofertas_c(k).Condicion_ejecucion = Con_ejec{ind_cond(k),1};
ofertas_c(k).Condicion_validez = Con_val{ind_cond(k),1};
ofertas_c(k).Cantidad_reducida = Cantidad_reducida{ind_cond(k),1};
ofertas_c(k).PPD = PPD{ind_cond(k),1};
ofertas_c(k).Fecha_envio = Fecha_envio{ind_cond(k),1};
ofertas_c(k).Fecha_cambiada= fecha_cambiada{ind_cond(k),1};
end

채택된 답변

Jan
Jan 2021년 5월 31일
편집: Jan 2021년 5월 31일
I'd stay at the loop and only avoid repeated indexing:
for k = 1:length(ind_cond)
kk = ind_cond(k);
S.Fecha = Fecha{kk};
S.Contrato = Contrato{kk}{1};
S.Zona = Zona{kk};
S.Agente = Agente{kk};
S.Unidad = Unidad{kk};
S.Precio = Precio{kk};
S.Cantidad = Cantidad{kk};
S.Tipo_oferta = Tipo_oferta{kk};
S.Condicion_ejecucion = Con_ejec{kk};
S.Condicion_validez = Con_val{kk};
S.Cantidad_reducida = Cantidad_reducida{kk};
S.PPD = PPD{kk};
S.Fecha_envio = Fecha_envio{kk};
S.Fecha_cambiada = fecha_cambiada{kk};
ofertas_c(k) = S;
end
If all elements would be cell arrays, a calling the command struct() would be fine, but the field Contrato makes needs an extra command:
ofertas_c = struct( ...
'Fecha', Fecha, ...
'Contrato', cellfun(@(c) c{1}, Contrato, ...
'Zona', Zona, ...
... and so on
)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by