フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by