Comparing two differently-sized arrays

7 views (last 30 days)
Gabriel Stanley
Gabriel Stanley on 16 Feb 2022
Edited: Mohammad Sami on 23 Feb 2022
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
Timo Dietz on 22 Feb 2022
Hi, I had a look at your .mat file. What exactly should be compared? Each time frame definition provides a value array. But what do you mean with "comparing" - with respect to the time information? The "Result" row above isn't clear to me.
Gabriel Stanley
Gabriel Stanley on 22 Feb 2022
I've editied the original question to try to improve clarity, By "comparing" I mean perform any operation/function on the data covering a given span of real-world time. Note that everything I've provided are manually-created examples. The number of elements in either array, the exact timestamps of either array, the total amount of time covered in either array, etc. can all vary. I am only assured that both arrays will cover roughly the same time (i.e. both will cover roughly the same 1/2 hour from 08:00-08:30, the same two hours from 17:13-19:17, etc.).

Sign in to comment.

Answers (1)

Mohammad Sami
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.
Some examples

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!