Trying to apply conditionals
Mostrar comentarios más antiguos
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 comentarios
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
el 24 de Nov. de 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.
JAmyl Andino
el 24 de Nov. de 2019
dpb
el 24 de Nov. de 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
el 24 de Nov. de 2019
Great! Please make sure to mark this question as answered.
Respuestas (0)
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!