From: Shaun Hurley on
I'm not sure if it's a dummy matrix that I need, but if not maybe you could suggest something else. I have made a program that will find the ranges of a set of data points. If the the first range is less than or equal to the 2nd, the two data points that make up that range are removed from that set of data. Is there a way to have all of these deleted numbers collected and set up as a new matrix? My ultimate goal is to have the program give me the mean and range of each set of data points that were deleted. Here is my code and thanks in advance for any input you have!

clc
clear all

x= [80 20 60 -100 40 -20 80];

q=length(x)-1; % number of elements in x
p=1; % starting with x(1)

% for p=p:q
% r(p)=abs(x(p)-x(p+1));
% p=p+1;
% end



p=1;

while p<=q % (or maybe p<=q) loop from p to q
r(p)=abs(x(p)-x(p+1)); % calculate 1st range (to be compared w/ 2nd)
r(p+1)=abs(x(p+1)-x(p+2)); % calculate 2nd range (to be compared w/ 1st)
if r(p)<=r(p+1) % if 1st number is <= to 2nd
x(p) = []; % erase 1st x value
x(p) = []; % erase 2nd x value
x % recalculate x after deleting x's
r=[];
for p=1:length(x)-1
r(p)=abs(x(p)-x(p+1)); % calculate r vector
end
r
q = length(r)-1; % recalculate the number of elements in x
p=1;
else
x
p = p + 1;
end
end
From: Matt Fig on
Your explanation is a little foggy. Could you give an example of the input and what you expect the output to be? Try to come up with an example which is:

1. Short, YET:
2. Covers all cases and exceptions you can think of, and
3. Give the expected size of the actual inputs you will use once the code is working.

Give both input and output.
From: Shaun Hurley on
No problem here is each step of the program:
x= [80 20 60 -100 40 -20 80]
1. Calculate the range between each set of numbers:
r= [60 40 160 140 60 100]
2. Locate the first range that is smaller than its subsequent range
r= [60 40 160 140 60 100] (40 in this case)
What x values make up the range of 40? (20 and 60)
3. Delete 20 and 60 from the x data:
x = [80 -100 40 -20 80]
4. Recalculate the range:
r = [180 140 60 100]

repeat

2. Locate the first range that is smaller than its subsequent range
r= [180 140 60 100] (60 in this case)
What x values make up the range of 60? (40 and -20)
3. Delete 40 and -20 from the x data:
x = [80 -100 80]
4. Recalculate the range:
r = [180 180]

repeat

2. Locate the first range that is smaller than its subsequent range
r= [180 180] (180 in this case)
What x values make up the range of 180? (80 and -100)
3. Delete 80 and -100 from the x data:
x = [80]
4. Recalculate the range:
r = []

That is what I have done in the program

Now, I am looking for the data that was deleted:

20 60 range: 40 mean: 40
40 -20 range: 60 mean: 10
80 -100 range:180 mean: -10

Sorry, that was not short at all, but I think I might have made my questions a little more clear!
From: Shaun Hurley on
No problem here is each step of the program:
x= [80 20 60 -100 40 -20 80]
1. Calculate the range between each set of numbers:
r= [60 40 160 140 60 100]
2. Locate the first range that is smaller than its subsequent range
r= [60 40 160 140 60 100] (40 in this case)
What x values make up the range of 40? (20 and 60)
3. Delete 20 and 60 from the x data:
x = [80 -100 40 -20 80]
4. Recalculate the range:
r = [180 140 60 100]

repeat

2. Locate the first range that is smaller than its subsequent range
r= [180 140 60 100] (60 in this case)
What x values make up the range of 60? (40 and -20)
3. Delete 40 and -20 from the x data:
x = [80 -100 80]
4. Recalculate the range:
r = [180 180]

repeat

2. Locate the first range that is smaller than its subsequent range
r= [180 180] (180 in this case)
What x values make up the range of 180? (80 and -100)
3. Delete 80 and -100 from the x data:
x = [80]
4. Recalculate the range:
r = []

That is what I have done in the program

Now, I am looking for the data that was deleted:

20 60 range: 40 mean: 40
40 -20 range: 60 mean: 10
80 -100 range:180 mean: -10

Sorry, that was not short at all, but I think I might have made my questions a little more clear!
From: Matt Fig on
This would seem to do it as a first step. The data re-writing will kill you with very long vectors. I leave the commenting to you.

% Data
x = [80 20 60 -100 40 -20 80]



% Engine
cnt = 0;
F = cell(floor(length(x)/2),1);

while length(x)>1
idx = find(diff(abs(diff(x)))>=0,1,'first');
if isempty(idx)
break
end
cnt = cnt + 1;
F{cnt} = x(idx:idx+1);
x(idx:idx+1) = [];
end

F = F(1:cnt);
mn = cellfun(@mean,F)
rng = cellfun(@(x) abs(diff(x)),F)
 |  Next  |  Last
Pages: 1 2
Prev: Wind barb plots
Next: Vector Field