If statement does not work
显示 更早的评论
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 个评论
the cyclist
2021-5-24
编辑:the cyclist
2021-5-24
Can you upload the workspace data in a MAT file, such that we can run your code and see what is happening? It's a bit confusing, since you mention variables A, B, and C ... which do not appear in your code at all.
Andrea Sbaragli
2021-5-24
编辑:per isakson
2021-5-24
the cyclist
2021-5-24
Thanks for posting the data.
Can you please do two more things, to help us help you?
- Could you be very specific about where the output is giving you something you don't expect, and tell us what you did expect, and why? It is too vague for you to say, "at a certain point even if the if statement is true it does not work anymore"
- Could you post a small subset of the data that exhibits the problem? Your current code takes 3 minutes to run on my machine, and it would be faster to debug something smaller.
Andrea Sbaragli
2021-5-24
Andrea Sbaragli
2021-5-24
编辑:per isakson
2021-5-24
the cyclist
2021-5-24
I understand. Here is what I notice:
Your code never actually compares F2(79329,1) and F1(47416,1). When i == 79329, the value of j is 4106. Therefore, you only compare F2(79329,1) and F1(4106,1). That difference is not less than 1 second.
I don't know if you are incrementing j, and making comparisons, in the way you intend.
Andrea Sbaragli
2021-5-24
Andrea Sbaragli
2021-5-24
the cyclist
2021-5-24
What you intend your code to do is not perfectly clear to me.
Here is what I see it doing:
- Go systematically down the rows of F2, until the time is within 1 second of the first row of F1 (because j==1).
- The first time this happens is when i==31014.
- Fill the row 1 values of F1 into row 31014 of Int12
- Next, continue along F2 (from row i==31015), until the time is within 1 second of the second row of F1 (because now j==2)
- Because F2(31015,1) and F1(2,1) are within 1 second of each, fill the row 2 values of F1 into Int12(31015)
- ... and so on
You can already see that all rows of F2 will not be compared with all rows of F1, as you seem to expect.
Andrea Sbaragli
2021-5-25
the cyclist
2021-5-25
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.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
