Ismember and ways to implement it
Show older comments
Hey,I have two strings where every message from there has an arrival time. I have to compare the Messages from S1 with S2(strings both), as they are very big to compare with ismember I have to do it another way. Compare the first message from S1 with all the messages from S2 that came at the same time or 1 min before and after. Imagine one message from S1 has arrived at 5 min 40 sec. it should be compared with the message from S2 that came from the minut 4:40 and 6:40. The code that gave me the time from each message is:
N = size(AIS1,1)
p = size(AIS2,1)
TimeAIS1 = [];
TimeAIS2 = [];
for i=1:1:N
seq1=AIS1(i);
TimeAIS1 = [TimeAIS1,extractAfter(seq1,strlength(seq1)-4)];
DN = str2double(TimeAIS1);
dur1 = minutes(floor(DN/100)) + seconds(mod(DN,100));
end
for j=1:1:N
seq2=AIS2(j);
TimeAIS2 = [TimeAIS2,extractAfter(seq2,strlength(seq2)-4)];
DN2 = str2double(TimeAIS2);
dur2 = minutes(floor(DN2/100)) + seconds(mod(DN2,100));
end
7 Comments
What does "very big" mean in absolute values? Why is this a limitation for ismember?
"Imagine one message from S1 has arrived at 5 min 40 sec" - I have no idea what this means.
What does the shown code do? How is it connected to the problem you have? How do the inputs look like and what do you want to achieve?
The iterative growing of arrays is extremely expensive. Avoid this strictlya. You current code computes STR2DOUBLE for all values in each iteration. Do this once only:
TimeAIS1 = [];
for i=1:1:N
seq1=AIS1(i);
TimeAIS1 = [TimeAIS1,extractAfter(seq1,strlength(seq1)-4)];
DN = str2double(TimeAIS1); % Repeatedly for all former values?!
dur1 = minutes(floor(DN/100)) + seconds(mod(DN,100)); % Overwritten in each iteration?!
end
% Much better without a loop:
TimeAIS1 = extractAfter(AIS1, strlength(AIS1) - 4)
DN = str2double(TimeAIS1);
dur1 = minutes(floor(DN/100)) + seconds(mod(DN,100));
flashpode
on 15 Sep 2021
flashpode
on 15 Sep 2021
It feels like you've jumped into the description of your problem in the middle.
You have some strings and somehow a time is incorporated into each string. Do they look like this?
secondsPerDay = seconds(hours(24));
dt = datetime('now') + seconds(randi(secondsPerDay, 10, 1));
s = string(dt)
If not, please post a small set of strings (no more than 10-20) that are representative of the strings with which you're working.
You have a second set of strings (same format?) that you want to compare with the first set (or vice versa.) Please show us a small set of those strings.
Now that we know the format of the data with which you're working, for those two small data sets describe what you want as output. Don't worry about how to implement it in MATLAB; explain your process for choosing what appears in the output. Is it just the timestamp information that's relevant for deciding whether something from a data set ends up in the output or are other parts of the string relevant?
flashpode
on 15 Sep 2021
Steven Lord
on 15 Sep 2021
So this message from your first set:
"!AIVDM,2,1,3,B,54hG=R82FP2e`LQc:208E8<v1HuT4LE:2222220U1pI446b;070PDPiC3kPH,0*720000"
would be compared with each of these messages from your second set:
"!AIVDM,1,1,,A,13ErMfPP00P9rFpGasc>4?wn2802,0*070000"
"!AIVDM,1,1,,B,13FMMd0P0009o1jGapD=5gwl06p0,0*780000"
"!AIVDM,1,1,,A,4028ioivDfFss09kDvGag6G0080D,0*790000"
"!AIVDM,1,1,,A,D028ioj<Tffp,0*2C0000"
"!AIVDM,1,1,,B,19NS@=@01qP9tp4GQkJ0bh`200SP,0*780000"
"!AIVDM,1,1,,B,137FrD0v2u0:=4pGS;s6u5On00SJ,0*000000"
"!AIVDM,1,1,,A,4028jJ1vDfG0009cIVGdh2?0280S,0*400000"
"!AIVDM,1,1,,B,H3GQ9khl4LLTF0l5T0000000000,2*070001"
"!AIVDM,1,1,,A,H33mw2Q>uV0luHTpN3800000000,2*080001"
"!AIVDM,1,1,,B,13FtuD?P00P9tuDGbFw4Jgv40L1f,0*030002"
In this case that first message is much longer than any of the messages from the second set, so there's no possibility of a match. Correct?
The second message in the first set also gets compared with the whole second set:
"!AIVDM,2,2,3,B,88888888880,2*240000"
By inspection, there's no match. A quick scan suggests the first message from the first set that matches is the 8th:
"!AIVDM,1,1,,A,4028ioivDfFss09kDvGag6G0080D,0*790000"
This matches the third message from the second set. So what do you want the output to look like? If you're emulating ismember you'd want the first seven elements of the first output to be false and the 8th true? For the second output you'd want the first seven elements to be 0 and the 8th 3?
flashpode
on 15 Sep 2021
Answers (0)
Categories
Find more on MATLAB 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!