How can reduce code?

4 次查看(过去 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 中查找有关 Read, Write, and Modify Image 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by