Create a code to compare two strings
1 次查看(过去 30 天)
显示 更早的评论
Okay what I want to do is to compare two doc of txt one is:
AIS1:
"!AIVDM,2,1,3,B,54hG=R82FP2e`LQc:208E8<v1HuT4LE:2222220U1pI446b;070PDPiC3kPH,0*720000"
"!AIVDM,2,2,3,B,88888888880,2*240000"
"!AIVDO,1,1,,,B3EkBN03wk?8mP=18D3Q3wv5sP06,0*230000"
"!AIVDM,1,1,,B,13GPhM0P01P9rGNGast>4?wn2@S7,0*7D0000"
"!AIVDM,1,1,,A,13ErMfPP00P9rFpGasc>4?wn2802,0*070000"
"!AIVDM,1,1,,A,33iMjv5P00P9wKdGcEOv4?v02DU:,0*460000"
"!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,,A,13MAj;P00<P<:hJGQecr`K820@0J,0*280000"
"!AIVDM,1,1,,B,19NS@=@01qP9tp4GQkJ0bh`200SP,0*780001"
"!AIVDM,1,1,,B,137FrD0v2u0:=4pGS;s6u5On00SJ,0*000001"
"!AIVDM,1,1,,A,4028jJ1vDfG0009cIVGdh2?0280S,0*400001"
"!AIVDO,1,1,,,B3EkBN03wk?8mP=18D3Q3wv5sP06,0*230001"
"!AIVDM,1,1,,B,H3GQ9khl4LLTF0l5T0000000001,2*060001"
"!AIVDM,1,1,,A,H33mw2Q>uV0luHTpN3800000000,2*080001"
"!AIVDO,1,1,,,B3EkBN03wk?8mP=18D3Q3wv5sP06,0*230002"
"!AIVDM,1,1,,B,13FtuD?P00P9tuDGbFw4Jgv40L1f,0*030002"
"!AIVDM,1,1,,A,13EoPo7P00P:0IdGc:d0@?v42<1?,0*560002"
"$AITXT,01,01,60,AIS: Internal GNSS not in use*280002"
"!AIVDM,1,1,,B,D028jJ03`N?b<`O6Dl<O6D0,2*350002"
"!AIVDM,1,1,,B,137JlD51h0P9tddGbCQSm0j2081e,0*0E0003"
"!AIVDO,1,1,,,B3EkBN03wk?8mP=18D3Q3wv5sP06,0*230003"
And the other AIS2:
"!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"
"!AIVDM,1,1,,B,D028jJ03`N?b<`O6Dl<O6D0,2*350002"
"!AIVDM,1,1,,B,137JlD51h0P9tddGbCQSm0j2081e,0*0E0002"
"!AIVDM,1,1,,A,13EuB00P0009n`TGb82:ugv600RQ,0*110003"
"!AIVDM,1,1,,A,13EsReP00009vQ`Gbj65gPh400SJ,0*350003"
"!AIVDM,1,1,,B,33EtT>5000P9nI2Gb8H5FP<60Dm:,0*580004"
"!AIVDM,1,1,,B,13Efqs800109q6DGb0wHhq@826p0,0*0A0004"
"!AIVDM,1,1,,A,13F9RTPP00P9rKpGasQf4?v820Sf,0*6D0004"
"!AIVDM,1,1,,B,13EmCs70010:0;bGc<Lbh3280<14,0*340004"
"!AIVDM,1,1,,A,13EcsW7P00P:07PGc9Ws@gv82D0l,0*060004"
"!AIVDM,1,1,,B,13EpM3PP0009nVPGb9EJG?v:0<1N,0*590005"
"!AIVDM,1,1,,A,15AMJH00000:1i6Ga0oP0Af:0<16,0*470005"
"!AIVDM,1,1,,A,13cq;9000GP9sUlGb2IPRPb680T5,0*510005"
"!AIVDM,1,1,,B,4028j;1vDfG0509cFhGdh5Q0083a,0*5C0006"
"!AIVDM,1,1,,B,D028j;0flffp,0*400006"
"!AIVDM,1,1,,A,137FrD032u0:=5<GS;6Vu5N:0<1=,0*620006"
"!AIVDM,1,1,,B,13Esmv000009qW:Gb=BLHAT@0H4>,0*660007"
"!AIVDM,1,1,,A,13E`977P00P:06DGc8D00?v>26p0,0*2B0007"
if they are the same put them in another variable I called it M_conjunt and if they are diferent in Two different variables one for the ones that are from AIS1 and another one that is for AIS2 I called them M_Dif_AIS1 and M_Dif_AIS2.
they are strings
and I want to compare every message with the other document I used the function strcmp
2 个评论
Rik
2021-7-15
If you insist on strcmp, what is your question? You will simply need a loop.
Otherwise, you might consider using ismember instead.
采纳的回答
Jayant Gangwar
2021-7-15
You can save the strings of each doc in two different vector of string, lets say S1 & S2 and then loop over for all the strings of vector 1 and compare it to vector of strings 2. If any of the string in vector 2 will be equal to the string in vector 1 then strcmp will return 1 at it's index and 0 at all the other indexes.
s=["abc","abcde"];
s2=["abc","ab","abcd"];
a=strcmp(s2,s(1));
this returns a = [1,0,0];
You can loop over for all of the strings in vector 1 and check using strcmp if that string is present in vector 2 and if yes then you can save it wherever you want for your preferred functionality;
3 个评论
Jayant Gangwar
2021-7-15
If you are using strcmp then you will have to deal with the 0 using loop otherwise you can use ismember function to get rid of the extra zeroes and just get the 1 if it is present and 0 if it is not.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!