min MAX of several variables

14 ビュー (過去 30 日間)
francesco
francesco 2013 年 12 月 4 日
編集済み: dpb 2013 年 12 月 4 日
is it possible to calculate in only one function, the min or MAX value for several variables? as for:
zal1, zal2, zal3, agg1, zgg2, zgg3 (are matrices nxn)
%
the bruteforce solution:
%
max(zal1,zal2);
max(ans,zal3);
max(ans,zgg1);
max(ans,zgg2);
maxz=max(max(max(ans,zgg3)))
%
min(zal1,zal2);
min(ans,zal3);
min(ans,zgg1);
min(ans,zgg2);
minz=min(min(min(ans,zgg3)))
is there any lean solution?

採用された回答

dpb
dpb 2013 年 12 月 4 日
編集済み: dpb 2013 年 12 月 4 日
If same size,
tmp=[zal1, zal2, zal3, agg1, zgg2, zgg3 ];
maxz=max(tmp(:));
minz=min(tmp(:));
Unfortunate can't use the (:) after the concatenation to avoid the temporary although since there are two operations it saves building twice as I doubt that the optimizer would figure out the optimization on its own.
ADDENDUM:
The whole problem illustrates why would be better, perhaps to rearrange the data structure to use cell arrays or named structures or higher dimension arrays instead of generating a series of sequentially-named variables. Then could eliminate the need to manipulate the separate variables explicitly.
ADDENDUM 2:
tmp=[zal1(:) zal2(:) zal3(:) agg1(:) zgg2(:) zgg3(:)];
gets the vector at the first go which may make a tiny speedup compared to above.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by