Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up ''ismember'' in Matlab

I'm using the Matlab build in command ''ismember'' to see whether a certain data set is within a larger dataset. The purpose of the piece of code is to remove any multiples of the [0 0] row from the larger data set shown below:

To do achieve this, I am using the following piece of code:

[Lia,locB] = ismember([0 0; 0 0],AFdata,'rows');
if sum(Lia) > 1 
AFdata(locB(1):locB(end-1),:) = [];

end

AFdata = [
1.0000   -0.0114
0.9975   -0.0098
0.9951   -0.0084
0.9928   -0.0074
0.9903   -0.0066
0.9804   -0.0042
0.9705   -0.0018
0.9606    0.0004
0.9507    0.0025
0.9408    0.0045
0.9309    0.0063
0.9210    0.0082
0.9111    0.0100
0.9012    0.0118
0.8913    0.0135
0.8814    0.0152
0.8715    0.0167
0.8616    0.0183
0.8517    0.0199
0.8418    0.0214
0.8318    0.0229
0.8219    0.0243
0.8120    0.0256
0.8021    0.0269
0.7922    0.0282
0.7823    0.0294
0.7724    0.0306
0.7625    0.0318
0.7526    0.0329
0.7427    0.0340
0.7328    0.0350
0.7229    0.0359
0.7130    0.0368
0.7031    0.0377
0.6932    0.0385
0.6833    0.0393
0.6734    0.0401
0.6635    0.0408
0.6536    0.0415
0.6437    0.0422
0.6338    0.0428
0.6239    0.0434
0.6140    0.0439
0.6041    0.0444
0.5942    0.0449
0.5843    0.0454
0.5744    0.0458
0.5645    0.0461
0.5546    0.0465
0.5447    0.0469
0.5348    0.0472
0.5249    0.0475
0.5150    0.0478
0.5051    0.0481
0.4951    0.0483
0.4852    0.0485
0.4753    0.0487
0.4654    0.0489
0.4555    0.0491
0.4456    0.0492
0.4357    0.0493
0.4258    0.0494
0.4159    0.0495
0.4060    0.0495
0.3961    0.0495
0.3862    0.0495
0.3763    0.0495
0.3664    0.0494
0.3565    0.0493
0.3466    0.0492
0.3367    0.0491
0.3268    0.0490
0.3169    0.0488
0.3070    0.0486
0.2971    0.0484
0.2872    0.0482
0.2773    0.0479
0.2674    0.0476
0.2575    0.0473
0.2476    0.0469
0.2377    0.0465
0.2278    0.0461
0.2179    0.0457
0.2080    0.0452
0.1981    0.0447
0.1882    0.0441
0.1783    0.0435
0.1684    0.0428
0.1584    0.0421
0.1485    0.0413
0.1386    0.0404
0.1287    0.0395
0.1188    0.0385
0.1089    0.0374
0.0990    0.0363
0.0891    0.0352
0.0792    0.0338
0.0693    0.0323
0.0594    0.0306
0.0495    0.0287
0.0396    0.0265
0.0297    0.0239
0.0198    0.0204
0.0099    0.0153
0.0050    0.0115
0.0020    0.0075
     0         0
     0         0
0.0020   -0.0075
0.0050   -0.0115
0.0099   -0.0153
0.0198   -0.0204
0.0297   -0.0239
0.0396   -0.0265
0.0495   -0.0287
0.0594   -0.0306
0.0693   -0.0323
0.0792   -0.0338
0.0891   -0.0352
0.0990   -0.0363
0.1089   -0.0375
0.1188   -0.0386
0.1287   -0.0396
0.1386   -0.0405
0.1485   -0.0414
0.1584   -0.0422
0.1684   -0.0429
0.1783   -0.0436
0.1882   -0.0442
0.1981   -0.0448
0.2080   -0.0454
0.2179   -0.0459
0.2278   -0.0463
0.2377   -0.0467
0.2476   -0.0471
0.2575   -0.0475
0.2674   -0.0478
0.2773   -0.0481
0.2872   -0.0484
0.2971   -0.0486
0.3070   -0.0488
0.3169   -0.0490
0.3268   -0.0491
0.3367   -0.0492
0.3466   -0.0493
0.3565   -0.0493
0.3664   -0.0493
0.3763   -0.0493
0.3862   -0.0492
0.3961   -0.0491
0.4060   -0.0490
0.4159   -0.0488
0.4258   -0.0486
0.4357   -0.0484
0.4456   -0.0481
0.4555   -0.0478
0.4654   -0.0474
0.4753   -0.0470
0.4852   -0.0465
0.4951   -0.0460
0.5051   -0.0455
0.5150   -0.0449
0.5249   -0.0442
0.5348   -0.0435
0.5447   -0.0427
0.5546   -0.0418
0.5645   -0.0408
0.5744   -0.0397
0.5843   -0.0386
0.5942   -0.0374
0.6041   -0.0362
0.6140   -0.0350
0.6239   -0.0337
0.6338   -0.0324
0.6437   -0.0310
0.6536   -0.0296
0.6635   -0.0281
0.6734   -0.0266
0.6833   -0.0252
0.6932   -0.0236
0.7031   -0.0220
0.7130   -0.0204
0.7229   -0.0188
0.7328   -0.0172
0.7427   -0.0156
0.7526   -0.0141
0.7625   -0.0125
0.7724   -0.0110
0.7823   -0.0095
0.7922   -0.0080
0.8021   -0.0067
0.8120   -0.0055
0.8219   -0.0045
0.8318   -0.0035
0.8418   -0.0026
0.8517   -0.0018
0.8616   -0.0012
0.8715   -0.0007
0.8814   -0.0004
0.8913   -0.0003
0.9012   -0.0004
0.9111   -0.0007
0.9210   -0.0012
0.9309   -0.0020
0.9408   -0.0030
0.9507   -0.0042
0.9606   -0.0055
0.9705   -0.0072
0.9804   -0.0092
0.9903   -0.0115
0.9928   -0.0119
0.9951   -0.0121
0.9975   -0.0119
1.0000   -0.0114]

However, this piece of code is executed for multiple datasets and numerous iterations which makes this a slows piece of code.

Is there any alternative to using ''ismember''? Or a quicker way to do this. Unfortunately I am not good with programing.

like image 488
Balraj Boyal Avatar asked Dec 14 '22 09:12

Balraj Boyal


2 Answers

If you use logical indexing it will be faster.

%create index
index = sum(AFdata' == 0)==2;
%clean AFdata
AFdata(index,:) = [];
like image 156
obchardon Avatar answered Jan 01 '23 23:01

obchardon


This is a minor improvement of obchardon's answer. There is no need to transpose the data set, instead use the dimension argument for sum or all. find is unnecessary, use logical indexing instead. Using all instead of sum makes the comparison unnecessary.

index =all(AFdata==0,2);
AFdata(index,:) = [];
like image 29
Daniel Avatar answered Jan 01 '23 21:01

Daniel