# How to do this in MATLAB without for loops

1 view (last 30 days)
S. David on 12 Mar 2012
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Daniel Shub on 12 Mar 2012
What about using an Nx4 matrix (or a cell array) instead of x1, x2, ...
In general x1, x2, ... naming is a bad idea:
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Andrei Bobrov on 12 Mar 2012
EDIT
n = 4;
a = eval(['[',sprintf('x%d,',1:n),']']);
[~,idx] = sort(min(a),'descend');
out = a(:,idx(1:2))
##### 4 CommentsShowHide 3 older comments
Jan on 13 Mar 2012
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.

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by