I have to switch the max and min elements in a vector and assume vec = ceil(rand(1, 10)*100) is a vector containing 10 random generated integers. The script i have to provide will swap the maximum and the minimum element in the vector.
Show older comments
I don't even know where to start, any help? Example: vec=[ 16 98 96 49 81 15 43 92 80 96], you script will result in vec = [ 16 15 96 49 81 98 43 92 80 96]
Answers (3)
ANKUR KUMAR
on 28 Sep 2018
Edited: ANKUR KUMAR
on 28 Sep 2018
Firstly, please follow Walter Roberson's comment.
A=[ 16 98 96 49 81 15 43 92 80 96]
A([find(A==min(A)),find(A==max(A))])=ones(1,length(find(A==min(A))))*max(A),ones(1,length(find(A==max(A))))*min(A)]
5 Comments
Bruno Luong
on 28 Sep 2018
This answer won't work if there is tie in min or max
ANKUR KUMAR
on 28 Sep 2018
If there is a tie in min and max, then all elements of matrix must be same.
Bruno Luong
on 28 Sep 2018
Edited: Bruno Luong
on 28 Sep 2018
Sorry for confused statement, "tie" means there is more than one element of the array reach the min-value (not minvalue==maxvalue), eg
a = [1, 3, 2, 1, 10]
Same issue with max. The array is not constant and your code just crashes
ANKUR KUMAR
on 28 Sep 2018
Oh sorry. I forgot to take this into consideration. This will work.
A = [1, 3, 2, 1, 10]
A([find(A==min(A)),find(A==max(A))])=[ones(1,length(find(A==min(A))))*max(A),ones(1,length(find(A==max(A))))*min(A)]
ANKUR KUMAR
on 28 Sep 2018
I edited my first answer.
Bruno Luong
on 28 Sep 2018
Solution for the problem of "swapping" ties min-values with ties max-values
minA = min(A);
maxA = max(A);
ismax = A==maxA;
A(A==minA) = maxA;
A(ismax) = minA;
1 Comment
Stephen23
on 28 Sep 2018
+1 probably the neatest way to do this.
Even though this is homework without any attempt... here is a neat and efficient MATLAB way to do that (this swaps the first minimum and the first maximum):
>> vec = [16,98,96,49,81,15,43,92,80,96]
vec =
16 98 96 49 81 15 43 92 80 96
>> [~,idn] = min(vec);
>> [~,idx] = max(vec);
>> vec([idn,idx]) = vec([idx,idn])
vec =
16 15 96 49 81 98 43 92 80 96
Of course you cannot hand this in as your own work, because that would be plagiarism.
3 Comments
ANKUR KUMAR
on 28 Sep 2018
Ops.. Why can not I able to write this at the very attempt? Still at learning stage.
You are worth MVP.
Bruno Luong
on 28 Sep 2018
Edited: Bruno Luong
on 28 Sep 2018
But your (Ankur's) code and Stephen's code do not perform the same arrangement in the case of tie. And poster never specify which one he actually needs.
Swap all tied maximum/minimum values:
>> vec = [15,16,96,96,49,81,15,43,92,80,96]
vec =
15 16 96 96 49 81 15 43 92 80 96
>> idn = find(min(vec)==vec);
>> idx = find(max(vec)==vec);
>> vec([idn,idx]) = vec([idx(1)*ones(1,numel(idn)),idn(1)*ones(1,numel(idx))])
vec =
96 16 15 15 49 81 96 43 92 80 15
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!