Optimize based on on max min of columns
1 view (last 30 days)
Show older comments
Hi,
I have an array where each row indicates the parameters of an item, I want to set constraints for each column (or parameter) and find the optimum item that closely meets these parameters. Here is an example,
%
A = [80 7.5 1000 0.5;120 500 3000 1;50 256 500 0.5];
For the first column I want the maximum, for the second I want the minimum, the third I want the max and the fourth I want the max. However as each row represents an item I need the item which closely matches the parameters I set. So far I have been trying to use a logic array that gives a 1 where the max/min value is and a zero where it does not meet the criteria then summing each row and the highest number will give the optimum. I have been trying to use a for loop to go through each column but I am having difficulty selecting both maximum and minimum across the array. So I would end up with an array like this,
%
A1 = [0 1 0 0;1 0 1 1;0 0 0 0]
The logical array indicates that the 2nd column is the optimum. If anyone has any advice on how to proceed, I would be very grateful.
1 Comment
Stephen23
on 10 May 2016
"indicates that the 2nd column is the optimum"
presumably you mean row, not column.
Answers (1)
Stephen23
on 10 May 2016
Edited: Stephen23
on 10 May 2016
With MATLAB using a loop to solve this is a waste of time. Try something like this:
>> A = [80 7.5 1000 0.5;120 500 3000 1;50 256 500 0.5]
A =
80 7.5 1000 0.5
120 500 3000 1
50 256 500 0.5
>> vec = max(A,[],1);
>> vec(2) = min(A(:,2))
vec =
120 7.5 3000 1
>> idx = bsxfun(@eq,A,vec)
idx =
0 1 0 0
1 0 1 1
0 0 0 0
>> [~,row] = max(sum(idx,2))
row = 2
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!