Trying to apply conditionals

Hi!, I have a simple array of four number a=[L1 L2 L3 L4] for which i already sorted the max(L) and min(s) numbers, I would like to sum the remaining two, so I'm trying to make the following condition in order to sort out those 2 numbers remaining. Here's what I'm doing:
if (L1>s) && (L1<L)
disp: (p)
This is not working but it's what I've seen on the tutorials or conditioning, any help?

6 Kommentare

dpb
dpb am 24 Nov. 2019
Bearbeitet: dpb am 24 Nov. 2019
L1 is a specific value which, as I understand what you wrote, may or may not be the min/max. If it so happens it is one or the other then the condition can't be true; it'll be equal to one or the other. Only if you happen to have picked one of the intermediate values will it be so.
Use the sorted array and the values between the first and last entries by index to retrieve those elements between min/max. Or, from the original array, return the sort order indices and use indirect addressing to retrieve from original array.
JAmyl Andino
JAmyl Andino am 24 Nov. 2019
I'm sorry, I'm just a beginner, I sorted the array in ascending order but, How do I retrieve the elements between min/max?
dpb
dpb am 24 Nov. 2019
Bearbeitet: dpb am 24 Nov. 2019
a=[L1 L2 L3 L4]; % an array
s=sort(a); % sort it
interior=s(2:end-1); % interior points are all except first/last
BTW, having multiple variables of same name with sequential suffixes as your L above is often a sign of not using Matlab array syntax to advantage. Probably what you should have is an array L that is built from the values of the four variables instead of creating four separate variables. Then you avoid the intermediate variable a. Also, other operations on the elements of L then can also use array syntax instead of explicit coding.
Thanks, tried it and worked like a charm!
%HW1 Barker's Classification
L1=input('Enter value for L1:');
L2=input('Enter value for L2:');
L3=input('Enter value for L3:');
L4=input('Enter value for L4:');
a=[L1, L2, L3, L4];
L=max(a);
disp(L);
s=min(a);
disp(s);
x=sort(a);
interior=x(2:end-1);
sum(interior);
sum(L+s);
dpb
dpb am 24 Nov. 2019
L(1)=input('Enter value for L1:');
L(2)=input('Enter value for L2:');
L(3)=input('Enter value for L3:');
L(4)=input('Enter value for L4:');
Lmx=max(L);
...
No sense in keeping the same data in two places -- you have it in L1 thru L4 and then just copy it to an array. Make the array in the first place..."It's the MATLAB way!" :)
Ridwan Alam
Ridwan Alam am 24 Nov. 2019
Great! Please make sure to mark this question as answered.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Produkte

Version

R2019b

Gefragt:

am 24 Nov. 2019

Kommentiert:

am 24 Nov. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by