Filter löschen
Filter löschen

How can reduce code?

1 Ansicht (letzte 30 Tage)
FRANCISCO JAVIER
FRANCISCO JAVIER am 31 Mai 2021
Bearbeitet: Jan am 31 Mai 2021
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

Akzeptierte Antwort

Jan
Jan am 31 Mai 2021
Bearbeitet: Jan am 31 Mai 2021
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
)

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by