Matching the size of two matrices based on values of a column
1 view (last 30 days)
Show older comments
Bryan Ikeda
on 14 Nov 2020
Commented: Peter Perkins
on 8 Dec 2020
I'm trying to make two matrices have the same dimensions, by matching up the values in the third column. The third column contains time in a day in decimal form. One of the matrix only runs for a certain time, and I want the 2nd matrix to match that length and line up the times. I'm currently using the code: where B is the matrix with the 24hr time frame and A has a smaller timeframe.
function [refined] = samesized( A, B)
[row,~] = size(A);
[rows,cols] = size(B);
re = zeros(rows,cols);
i = 1;
j = 1;
while i < row
while j < rows
if A(i,3) == B(j,3)
refined(i,3) = B(j,3);
refined(i,2) = B(j,2);
refined(i,1) = B(j,1);
end
j = j+1;
end
j = 1;
i = i+1;
end
refined( ~any(refined,2), : ) = [];
end
However, it sometimes skips values in the output and actually makes the returned matrix smaller, even though the values in column matched. I was wondering if anyone could help me figure out how to fix this, or know of a better way to do it.
Thanks!
0 Comments
Accepted Answer
Peter Perkins
on 19 Nov 2020
"contains time in a day in decimal form": That would be your problem. Use timetables with datetimes (or maybe durations?), and use the [inner|outer]join function.
>> tt1 = timetable(rand(10,1),'RowTimes',datetime(2020,11,1:10)+hours(1:10));
>> tt2 = timetable(rand(5,1),'RowTimes',datetime(2020,11,1:2:10)+hours(1:2:10));
>> tt12 = innerjoin(tt1,tt2,'Key','Time')
tt12 =
5×2 timetable
Time Var1_tt1 Var1_tt2
____________________ ________ ________
01-Nov-2020 01:00:00 0.13755 0.29541
03-Nov-2020 03:00:00 0.6501 0.99014
05-Nov-2020 05:00:00 0.32355 0.16225
07-Nov-2020 07:00:00 0.68174 0.44102
09-Nov-2020 09:00:00 0.54227 0.55718
2 Comments
Peter Perkins
on 8 Dec 2020
Bryan, you can store the timestamps as datetimes, and then convert to "fractional days" in the calculation. Something like
seconds(timeofday(tt12.Time)) / 86400
More Answers (1)
ICR
on 14 Nov 2020
Hi,
This should work.
% Generate random integers
B = randi(24,[24 3]);
B(:,3) = 24:-1:1; % Replace these two your data
A = randi(15,[15 3]);
A(:,3) = randperm(15,15)'; % Replace these two your data
[r1,c1] = size(A)
outputMat = zeros(r1,c1); % Initialise
for i =1:1:r1
[row,~] = find(B(:,3) == A(i,3))
outputMat(i,:) = B(row,:);
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!