If statement does not work

3 views (last 30 days)
Andrea Sbaragli
Andrea Sbaragli on 24 May 2021
Commented: the cyclist on 25 May 2021
I have a problem in populating correctly a table. Starting from A(mx4) and B(nx4), where m>n, I have to create C(mx4). According to the code whether the if statement it is not true 4 coloumn have default value.
function [Int12] = bundle(F1,F2)
A= zeros(height(F2), 8);
Int12= array2table(A);
Int12.A2 = NaT(length(A),1,'Format', 'dd-MMM-yyyy HH:mm:ss.SSSSSSSSS' );
Int12.A6 = NaT(length(A),1, 'Format', 'dd-MMM-yyyy HH:mm:ss.SSSSSSSSS' );
j=1;
for i=1: height(F2)
Int12(i,5)= F2(i,2); % tag
Int12(i,6)= F2(i,1); % timestamp
Int12(i,7)= F2(i,3); % X
Int12(i,8)= F2(i,4); % Y
if abs(F2{i,1}- F1{j,1})<= duration('00:00:01.000000000')
Int12(i,1)= F1(j,2); % tag
Int12(i,2)= F1(j,1); % timestamp
Int12(i,3)= F1(j,3); % X
Int12(i,4)= F1(j,4); % Y
j=j+1;
end
end
end
The problem is that the function works but at a certain point even if the if statement is true it does not work anymore.
  11 Comments
Andrea Sbaragli
Andrea Sbaragli on 25 May 2021
Yes that's right.
For this purpose I have modified the code as it follows but, as you probably now better than me, it is too expensive computationally...
function [Int12] = bundle(F1,F2)
A= zeros(height(F2), 8);
Int12= array2table(A);
Int12.A2 = NaT(length(A),1,'Format', 'dd-MMM-yyyy HH:mm:ss.SSSSSSSSS' );
Int12.A6 = NaT(length(A),1, 'Format', 'dd-MMM-yyyy HH:mm:ss.SSSSSSSSS' );
j=1;
for i=1: (height(F2))
Int12(i,5)= F2(i,2); % tag
Int12(i,6)= F2(i,1); % timestamp
Int12(i,7)= F2(i,3); % X
Int12(i,8)= F2(i,4); % Y
for j= 1: height(F1)
if abs(F2{i,1}- F1{j,1})<= duration('00:00:01.000000000')
Int12(i,1)= F1(j,2); % tag
Int12(i,2)= F1(j,1); % timestamp
Int12(i,3)= F1(j,3); % X
Int12(i,4)= F1(j,4); % Y
end
end
end
end
the cyclist
the cyclist on 25 May 2021
It is no surprise to me that your code is computationally expensive. You are comparing 79329 elements to 47614 elements. That is simply a large number of comparisons.
Also, if all of your times were close to each other, you would be creating a table that is over 3 billion rows long (79329x47614). I don't know how big it will actually be, but I expect it is much larger than the memory you have preallocated.
You haven't really described the overall intent of your code. Do you really need to store every instance where a row of F2 is close in time to F1? Or maybe your output is actually something simpler and smaller.
What does the result need to be? It is possible that there is a faster way to do it.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!