How to do this in MATLAB without for loops

2 ビュー (過去 30 日間)
S. David
S. David 2012 年 3 月 12 日
Hi,
I have 4 vectors x1, x2, x3, and x4 all of size N-by-1. I need to select two vectors such that their minimum values are the maximum among all. How can I do that in MATLAB without using for loops?
Thanks

回答 (2 件)

Daniel Shub
Daniel Shub 2012 年 3 月 12 日
What about using an Nx4 matrix (or a cell array) instead of x1, x2, ...
In general x1, x2, ... naming is a bad idea:

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 12 日
EDIT
n = 4;
a = eval(['[',sprintf('x%d,',1:n),']']);
[~,idx] = sort(min(a),'descend');
out = a(:,idx(1:2))
  4 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 3 月 13 日
Hi Jan! Corrected for general (any) case, when n > 4.
Jan
Jan 2012 年 3 月 13 日
There is even no general case of x1, x2, x3, ... as long as Matlab cannot handle x_inf.
I think I should publish a Mex function "Unpoof('x', a, b)", which concatenates the contents of the variables x<a> to x<b> to the variable x and use the ASSIGNIN equivalent to put x into the caller workspace.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by