Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Looking for help with dummy matrix

Subject: Looking for help with dummy matrix

From: Shaun Hurley

Date: 7 Jul, 2010 15:53:04

Message: 1 of 8

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

Subject: Looking for help with dummy matrix

From: Matt Fig

Date: 7 Jul, 2010 16:04:05

Message: 2 of 8

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.

Subject: Looking for help with dummy matrix

From: Shaun Hurley

Date: 7 Jul, 2010 16:33:06

Message: 3 of 8

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!

Subject: Looking for help with dummy matrix

From: Shaun Hurley

Date: 7 Jul, 2010 16:35:19

Message: 4 of 8

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!

Subject: Looking for help with dummy matrix

From: Matt Fig

Date: 7 Jul, 2010 17:10:19

Message: 5 of 8

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)

Subject: Looking for help with dummy matrix

From: Shaun Hurley

Date: 7 Jul, 2010 17:26:20

Message: 6 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <i12cdr$3s7$1@fred.mathworks.com>...
> 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)

That's amazing how you did that so fast. I have been working on this for over a week now. (I'm new to this) I really appreciate this. I just posted another message. I was able to pull out the ranges and means, but not as vectors. Do you know how to do that? I would like to stick with what I've done because I understand it. If I need to though, I will try and go through and understand your way. Thank you again so much!

Subject: Looking for help with dummy matrix

From: Matt Fig

Date: 7 Jul, 2010 18:29:05

Message: 7 of 8

You should be able to store your data in an array. Here is your code modified somewhat for efficiency and with the data stored in array H. H should have all the data you need to do whatever you need afterward.


p = 1;
q = length(x)-2; % number of elements in x
H = zeros(2,floor(length(x)/2)); % Store information here.
cnt = 0;

while p<=q % (or maybe p<=q) loop from p to q
    r(1) = abs(x(p)-x(p+1)); % calculate 1st range (to be compared w/ 2nd)
    r(2) = abs(x(p+1)-x(p+2)); % calculate 2nd range (to be compared w/ 1st)
    if r(1)<=r(2) % if 1st number is <= to 2nd
        cnt = cnt + 1;
        H(:,cnt) = x(p:p+1);
        x(p) = []; % erase 1st x value
        x(p) = []; % erase 2nd x value
        q = q-2; % recalculate the number of elements in x
        p = 1;
    else
        p = p + 1;
    end
end

H = H(:,1:cnt);
mn2 = mean(H); % The means.
rng2 = abs(diff(H)); % The ranges.

Subject: Looking for help with dummy matrix

From: Shaun Hurley

Date: 7 Jul, 2010 21:07:04

Message: 8 of 8

Thank you so much!! I was able to finish my entire program today! At the rate I was going it was literally going to take at least a month. Everything works perfect, thank you again!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us