Simple rank looping calculation with equation

2 views (last 30 days)
Hello,
I am a novice at Matlab (actually a medical student)
I am trying to write a script that will do the following:
1. Rank an array of a given amount of numbers from least value to highest. (ex. [2,5,7,15,16,19]
2. Using a simple second order equation (ex. x^2-5xy+6y^2), take the first (least value) and last number (highest value) of the array and plug them into an equation to get a new value.
3. Take the second lowest and second highest numbers of the array, run them through the same equation and spit out a new value.
4. Repeat for all numbers in the original array until a new smaller array is obtained. For this example, a 1x6 array would shrink to a 1x3.
5. Repeat this process again and again until only ONE number remains.
I want to be able to do this for any dimension array I start out with.
So far I have been able to do this only once. I am just adding the values instead of using a quadratic equation as I typed above. My script so far is this:
clear
a=[2,5,7,15,16,19];
b=sort(a);
c=size(b);
x=c(1);
y=c(2);
%while y>1
value(1)=b(1)+b(y);
for i=x:(y/2-1);
j=b(i+1)+b(y-i);
value(i+1)=j;
end
y=size(value,2);
%end
disp(value);
I'm thinking I need to create a while loop to run it while the size(array,2)>1. However I can't quite seem to do this. Anyone have any suggestions?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 19 Feb 2014
This is how I'd do it:
a=[2,5,7,15,16,19];
% Sort the input.
sorteda = sort(a);
% Find the middle element.
middleElement = ceil(length(a)/2)
for k = 1 : middleElement % Go half way.
% Get x and y.
x = sorteda(k)
y = sorteda(end-k+1)
% Compute formula and store in output.
output(k) = x^2 - 5*x*y + 6 * y ^ 2;
end
% Display output in command window.
output
  6 Comments
Gautam
Gautam on 19 Feb 2014
Script:
clear
a=[1,2,3,4,5];
while length(a)>1
a = CaliberFunction(a);
end
CaliberFunction:
function [a] = CaliberFunction(a)
% Sort the input.
sorteda = sort(a);
disp('this is too big')
% Find the middle element.
middleElement = ceil(length(a)/2);
for k = 1 : middleElement % Go half way.
x = sorteda(k)
y = sorteda(end-k+1)
a(k) = x^2 - 5*x*y + 6 * y ^ 2;
end
a
return
Image Analyst
Image Analyst on 20 Feb 2014
You don't understand how functions work. You need to study up on that. See how you changed what I called output to "a"? That's how you broke it. You don't replace a there in CaliberFunction, you replace it when you call the function. Corrected code is below (test2.m):
function test2
clc;
a=[1,2,3,4,5];
iteration = 1;
while length(a)>1
fprintf('For iteration #%d, length(a) = %d\na = ', iteration, length(a));
fprintf('%.4f, ', a);
fprintf('\n');
a = CaliberFunction(a)
iteration = iteration + 1;
end
% CaliberFunction:
function output = CaliberFunction(a)
% Sort the input.
sorteda = sort(a)
% Find the middle element.
middleElement = ceil(length(a)/2);
for k = 1 : middleElement % Go half way.
x = sorteda(k);
y = sorteda(end-k+1);
output(k) = x^2 - 5*x*y + 6 * y ^ 2;
end
output
return;

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!