MATLAB Answers

Removing the max and min of a vector without removing multiple minimums or maximums

10 views (last 30 days)
I'm working on a script for four-bar linkages and need to remove the shortest and longest links for calculations. However if my shortest or longest value is repeated in my vector, my indexing removes all of them. How do I prevent all being removed when I just need 1 min and 1 max removed
%Enter Link Lengths
l1 = 20; %Link 1, ground
l2 = 10; %Link 2, crank
l3 = 10; %Link 3, coupler
l4 = 10; %Link 4, rocker
a1 = [l1,l2,l3,l4]; %Array of link values for finding S & L
%Mobility
L = 4; %Number of links
J = 4; %Number of Joints
G = 1; %Number of Grounded links
M = 3*L-2*J-3*G
%Barker Classification
S=min(a1); %Shortest Link
L=max(a1); %Longest Link
a2=a1(a1~=S & a1~=L); %P & Q matrix
P=a2(1); Q=a2(2); %Pulling out P & Q

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 23 Sep 2020
Edited: Stephen Cobeldick on 23 Sep 2020
[~,Sx] = min(a1); % Shortest Link
[~,Lx] = max(a1); % Longest Link
a2 = a1;
a2([Sx,Lx]) = []
And tested using the values that you gave here:
>> l1 = 6; %Link 1, ground
>> l2 = 2; %Link 2, crank
>> l3 = 7; %Link 3, coupler
>> l4 = 9; %Link 4, rocker
>> a1 = [l1,l2,l3,l4]; %Array of link values for finding S & L
>> [S,Sx] = min(a1); %Shortest Link
>> [L,Lx] = max(a1); %Longest Link
>> a2 = a1;
>> a2([Sx,Lx]) = []
a2 =
6 7

  0 Comments

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 23 Sep 2020
Logan - from max value and indices you can get the index to the maximum (or minimum) value and then use that index to remove the element. For example,
[minValues, minIndices] = min(a1); %Shortest Link
[maxValues, maxIndices] = max(a1); %Longest Link
a2 = a1;
if ~isempty(minIndices)
a2(minIndices(1)) = []; % we use the first element of minIndices in case array length > 1
end
if ~isempty(maxIndices)
a2(maxIndices(1)) = []; % we use the first element of maxIndices in case array length > 1
end

  3 Comments

Logan Tate
Logan Tate on 23 Sep 2020
Thank you. However, when I input a set of numbers where this problem occur, I get an error with your code. "Matrix index is out of range for deletion"
How do I account for those cases as well?
%Enter Link Lengths
l1 = 6; %Link 1, ground
l2 = 2; %Link 2, crank
l3 = 7; %Link 3, coupler
l4 = 9; %Link 4, rocker
a1 = [l1,l2,l3,l4]; %Array of link values for finding S & L
%Barker Classification
S=min(a1); %Shortest Link
L=max(a1); %Longest Link
[minValues, minIndices] = min(a1); %Shortest Link
[maxValues, maxIndices] = max(a1); %Longest Link
a2 = a1;
if ~isempty(minIndices)
a2(minIndices(1)) = []; % we use the first element of minIndices in case array length > 1
end
if ~isempty(maxIndices)
a2(maxIndices(1)) = []; % we use the first element of maxIndices in case array length > 1
end
Stephen Cobeldick
Stephen Cobeldick on 23 Sep 2020
"I get an error with your code. "Matrix index is out of range for deletion""
If after removing the first element the second index refers to an element that no longer exists, then this answer will throw an error. See my answer for a working solution.
"we use the first element of minIndices in case array length > 1"
Why is that required?:
>> length(a1)
ans = 4
>> [~,maxIndices] = max(a1)
maxIndices = 4
>> [~,minIndices] = min(a1)
minIndices = 2
Geoff Hayes
Geoff Hayes on 23 Sep 2020
I wrongly assumed that minIndices would be an array of indices of all elements with the same minimum value. oops.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by