# Comparing two differently-sized arrays

9 views (last 30 days)

Show older comments

A'ight, I'm throwing in the towel. I have two different arrays, which are indexed by time. Each element of both arrays has two time indices: start time and stop time (The bars separating each cell in the below table image). These arrays are not necessarily the same size, nor can I assume that there will be any matches in their time indecises (and thus I can't rely on intersect). Because my result needs to be time-synched, I can't rely on best match, either. Below is a representation of about the worst-case scenario for what I'll need to be working with(Array1 and Array2), and what I want to end up with (Array1New and Array2New):

The greyed-out cells represnt spans of real-world time for which there is no data, nor any entries in the respective arrays. So Array1 starts and ends after Array2, and Array1 has no entry covering the span of time between elements 4 & 5. E.g.

Array1{1,:} = {data11} {[16-Feb-2022 08:17:29]} {[16-Feb-2022 08:27:12]}

Array2{1,:} = {data21} {[16-Feb-2022 08:00:33]} {[16-Feb-2022 08:20:18]}

Array1{4,:} = {data24} {[16-Feb-2022 10:23:38]} {[16-Feb-2022 10:37:56]}

Array1{5,:} = {data24} {[16-Feb-2022 10:52:31]} {[16-Feb-2022 11:06:15]}

I need to either reformat Array1 and Array2 to match Array1New and Array2 new, or find some way to call the desired sections of Array 1 and Array2 when performing operations wich use elements both Arrays as inputs. Put another way, because this seems to be very confusing to others:

Option A) Reformat Array1 and Array2 so that they cover the same real-world timeframe, have the same timestamps, and the same number of elements. New elements which don't map back to an old element (e.g. Array1New(1,1)) should be filled with pre-determined filler data, while elements which do map should have data copied over (e.g. Array1New(2,1)=Array1(1,1)).

Option B) Have some syntax or which allows me to achieve the net result of Option A without reshaping Array1 and Array2 (in case such would be faster/better).

Thus far, I have the following code note that my actual data inputs are structure arrays, hence the specific braces used.

...

TimeIdcs1 = [Record1.StartTime;Record1.StopTime]'

TimeIdcs2 = [Record2.StartTime;Record2.StopTime]'

%Combine timestamps into single vector, remove duplicates, and sort

TimeRecord=unique(vertcat(TimeIdcs1,TimeIdcs2));

%Use circshift to create single, combined timestamp array

TimeRecord(:,2)=circshift(TimeRecord,-1);

TimeRecord(end,:)=[];

%Create Array1New and Array2New

TimeRkrdLength = length(TimeRecord);

Array1New = cell(OvrLpRkrdLength,3);

Array2New = cell(OvrLpRkrdLength,3);

for i=1:TimeRkrdLength

Array1New{i,2}=TimeRecord(i,1);

Array1New{i,3}=TimeRecord(i,2);

Array1New{i,2}=TimeRecord(i,1);

Array1New{i,3}=TimeRecord(i,2);

end

%Define mapping of records using intersect if possible

[~,Array1Start,TimeRecordStart] = intersect(TimeIdcs1(:,1),TimeRecord(:,1));

[~,~,TimeRecordStop] = intersect(TimeIdcs1(:,2),TimeRecord(:,2));

for i=1:length(TimeRecordStart)

Array1New{TimeRecordStart(i):TimeRecordStop(i),i}=Array1(Array1Start(i))

%Not really sure how to do this, as there does not appear to be a way

%to block assign a set of values to an entire column of a cell array

end

##### 8 Comments

Timo Dietz
on 22 Feb 2022

### Answers (1)

Mohammad Sami
on 23 Feb 2022

Edited: Mohammad Sami
on 23 Feb 2022

The best way to do what you are requesting is to convert your arrays to timetables and then either use the synchronize function or the Live Script Synchronize timetable task to combine these timetables. You can find more details in the documentation here. As the function may have changed over time you can refer to the documentation for your version of Matlab.

R2020a+ Live Task https://www.mathworks.com/help/releases/R2021a/matlab/ref/synchronizetimetables.html

R2016b+ Synchronize function https://www.mathworks.com/help/releases/R2021a/matlab/ref/timetable.synchronize.html

Some examples

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!