How can reduce code?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Jan
le 31 Mai 2021
Modifié(e) : Jan
le 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
)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!