%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KENT STATE UNIVERSITY
% Analytical Methods, Fall 2013
%
% SCRIPT ANMT_Lec1_mySums
%
% This script creates a set of six random numbers U[0,1] divided into two
% sets: S1 and S2. The sum of each set is calculated, and depending on
% which sum is bigger, we will put these sets in the first 3 or the last 3
% cells of a new dataset.
%
% For example: S1=[.1 .2 .3]; S2=[.4; .5; .6]. Here the sum of S2>sum of S1.
% Consequently, the first row of our new dataset will be:
%
% [.4 .5 .6 .1 .2 .3]
%
% INPUT: None. Data is randomly generated.
%
% X(Nx6): X~U[0,1]. This is divided into two sets S1 and S2.
%
% OUTPUT: Xnew(Nx6): Reordered data.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear; clc; format bank;
%Determine data size
N=100;
%Generate 100 random numbers, X~U[0,1]
X=rand(N,6);
%Divide X into the sets S1 and S2.
S1=X(:,1:3);
S2=X(:,4:6);
%Create empty space for new data
Xnew=zeros(size(X)); %Note that we only say size(X) instead of size(X,1) or
%size(X,2) because the new dataset is of IDENTICAL
%size.
%%%%%%%%%%%
%ALGORITHM
%%%%%%%%%%%
%Scroll through the dataset and determine which sum is bigger. Based on
%this, assemble the new dataset accordingly.
for i=1:1:N
%Extract relevant variables from dataset
S1chunk=S1(i,:);
S2chunk=S2(i,:);
%Depending on which sum is bigger, assign chunks to new data
%accordingly.
if sum(S1chunk)>sum(S2chunk) %If S1 sum is bigger, S1 goes first
Xnew(i,1:3)=S1chunk;
Xnew(i,4:6)=S2chunk;
elseif sum(S2chunk)>sum(S1chunk) %If S2 sum is bigger, S2 goes first
Xnew(i,1:3)=S2chunk;
Xnew(i,4:6)=S1chunk;
else %If tie, assign them randomly by flipping a coin
%Draw a random number U~[0,1]
judge=rand();
%Depending on the coin flip, assign.
if judge<=.5
Xnew(i,1:3)=S1chunk;
Xnew(i,4:6)=S2chunk;
else
Xnew(i,1:3)=S2chunk;
Xnew(i,4:6)=S1chunk;
end
end
end
%Display new and old datasets
[X Xnew]