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:
Find Same Number in two matrices of varying size

Subject: Find Same Number in two matrices of varying size

From: Justin

Date: 23 Jun, 2010 02:05:09

Message: 1 of 3

Hello, I have a large dataset (100K+ rows) that has two time columns which represent a starting and ending time.

I wish to know for each hour (from serial numbers timeStart = 37988; and timeEnd = 39966;) how many occurrence exist within the dataset start and end time columns where the time being analysed (time = timeStart:1/24:timeEnd;) falls between the start and end time of the dataset.

This code does what I want but is not very elegant and takes way too long with larger datasets.

function [count] = calcCensus(timeStart,timeEnd,dataIn)
count = zeros(1,size(timeStart:1/24:timeEnd,2));
nCount = 0;
for n= timeStart:1/24:timeEnd
    n
    nCount = nCount + 1;
    temp=[];
    tempa = find(n < cell2mat(dataIn{2,1}(:,3)));
    tempb = find(n >= cell2mat(dataIn{2,1}(:,2)));
    aCount = 0;
    for a=1:size(tempb)
        
        if isempty(find(tempa == tempb(a), 1)) == 1
            %do nothing
        else
            aCount = aCount +1;
            temp(aCount) = find(tempa == tempb(a));
        end

    end
    clear tempa
    clear tempb
    count(nCount) = size(temp,2);
    
    
end

Sorry if the question is not well described. Any help is appreciated.

Subject: Find Same Number in two matrices of varying size

From: Roger Stafford

Date: 23 Jun, 2010 03:17:26

Message: 2 of 3

"Justin " <jstinnet@uvm.edu> wrote in message <hvrq4l$64s$1@fred.mathworks.com>...
> Hello, I have a large dataset (100K+ rows) that has two time columns which represent a starting and ending time.
>
> I wish to know for each hour (from serial numbers timeStart = 37988; and timeEnd = 39966;) how many occurrence exist within the dataset start and end time columns where the time being analysed (time = timeStart:1/24:timeEnd;) falls between the start and end time of the dataset.
>
> This code does what I want but is not very elegant and takes way too long with larger datasets.
>
> function [count] = calcCensus(timeStart,timeEnd,dataIn)
> count = zeros(1,size(timeStart:1/24:timeEnd,2));
> nCount = 0;
> for n= timeStart:1/24:timeEnd
> n
> nCount = nCount + 1;
> temp=[];
> tempa = find(n < cell2mat(dataIn{2,1}(:,3)));
> tempb = find(n >= cell2mat(dataIn{2,1}(:,2)));
> aCount = 0;
> for a=1:size(tempb)
>
> if isempty(find(tempa == tempb(a), 1)) == 1
> %do nothing
> else
> aCount = aCount +1;
> temp(aCount) = find(tempa == tempb(a));
> end
>
> end
> clear tempa
> clear tempb
> count(nCount) = size(temp,2);
>
>
> end
>
> Sorry if the question is not well described. Any help is appreciated.
- - - - - - - - -
  If I understand what you want, this might save some time:

 d2 = cell2mat(dataIn{2,1}(:,2));
 d3 = cell2mat(dataIn{2,1}(:,3));
 t = timeStart:1/24:timeEnd;
 for k = 1:length(t)
  count(k) = sum(d2<=t(k) & t(k)<d3);
 end

Roger Stafford

Subject: Find Same Number in two matrices of varying size

From: Justin

Date: 24 Jun, 2010 01:29:04

Message: 3 of 3

Robert, That works very nicely. Thank you!

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