-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewberg and Murphy Walkthrough.nb
7136 lines (6782 loc) · 342 KB
/
Newberg and Murphy Walkthrough.nb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Content-type: application/mathematica *)
(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)
(* CreatedBy='Mathematica 7.0' *)
(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[ 145, 7]
NotebookDataLength[ 343136, 7128]
NotebookOptionsPosition[ 317208, 6713]
NotebookOutlinePosition[ 320343, 6784]
CellTagsIndexPosition[ 320300, 6781]
WindowFrame->Normal*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[CellGroupData[{
Cell[TextData[StyleBox["Notes on \"A Framework for the Automated Analysis of \
Subcellular Patterns in Human Protein Atlas Images\"",
FontFamily->"Calibri",
FontSize->22]], "Title",
CellChangeTimes->{{3.4742891466582317`*^9, 3.474289301755433*^9}, {
3.474289524106206*^9, 3.474289556194205*^9}},
TextAlignment->Left,ExpressionUUID->"fb470410-9932-4148-9767-d3996f587ffb"],
Cell[TextData[{
StyleBox["This is a walkthrough of the \"unmixing\" path of the methods \
presented in \"A Framework for the Automated Analysis of Subcellular Patterns \
in Human Protein Atlas Images\", by Justin Newberg and Robert F. Murphy, \
published in the ",
FontFamily->"Cambria"],
StyleBox["Journal of Proteome Research",
FontFamily->"Cambria",
FontSlant->"Italic"],
StyleBox[", vol. 7, no. 6. pp. 2300-2308, (2008).",
FontFamily->"Cambria"]
}], "Text",
CellChangeTimes->{{3.4742891466582317`*^9, 3.474289301755433*^9}, {
3.474289524106206*^9, 3.474289556194205*^9}, {3.47428980355612*^9,
3.474289822177782*^9}, {3.47428998214911*^9, 3.47428998214911*^9}},
TextJustification->0.,ExpressionUUID->"c141aa0d-de15-4c6f-b6d2-879819da1e81"],
Cell[TextData[{
StyleBox["The authors kindly provided access to the source code and images \
used for the research presented in their paper. The web page for the paper is \
",
FontFamily->"Cambria"],
ButtonBox["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch \
_HPA/",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/"],
None},
ButtonNote->
"http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/"],
". The code was written in MATLAB and is available at ",
ButtonBox["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch \
_HPA/JPRAtlasCode.tar.gz",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/\
JPRAtlasCode.tar.gz"], None},
ButtonNote->
"http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/\
JPRAtlasCode.tar.gz"],
". The images (1.4 ",
StyleBox["GB",
FontWeight->"Bold",
FontSlant->"Italic"],
") are available from the Human Protein Atlas at ",
ButtonBox["http://proteinatlas.org/",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://proteinatlas.org/"], None},
ButtonNote->"http://proteinatlas.org/"],
" or from the author's web site at ",
ButtonBox["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch \
_HPA/JPRAtlasImages.tar.gz",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/\
JPRAtlasImages.tar.gz"], None},
ButtonNote->
"http://murphylab.web.cmu.edu/software/2008_JProteomeResearch_HPA/\
JPRAtlasImages.tar.gz"],
"."
}], "Text",
CellChangeTimes->{{3.474289984476818*^9, 3.4742900267036247`*^9}, {
3.4742900701333404`*^9, 3.4742900818656125`*^9}, {3.4742901776140766`*^9,
3.474290186721819*^9}, {3.474290219934616*^9, 3.474290240149743*^9}, {
3.47429029068756*^9, 3.474290304763162*^9}, {3.474290341631554*^9,
3.4742903817962303`*^9}, {3.474290419961265*^9, 3.474290459954097*^9}, {
3.474290529863442*^9, 3.4742905837909365`*^9}, {3.4742906284356422`*^9,
3.474290628545066*^9}, {3.474290757445713*^9, 3.474290779424165*^9}, {
3.474290904467084*^9, 3.474290904467084*^9}},
TextJustification->0.,ExpressionUUID->"1e9ec965-5a28-4436-b812-cfb2f1d6ad42"],
Cell[CellGroupData[{
Cell["System Requirements", "Section",
CellChangeTimes->{{3.4742909058108945`*^9, 3.474290913670622*^9}},
TextAlignment->Left,
FontFamily->"Calibri",
FontSize->16,ExpressionUUID->"92f745ba-b062-47ae-a2ba-14f94a45ff02"],
Cell["\<\
From the author's web site: \"The scripts were written for Matlab version 7.1 \
on Linux. The statistics, wavelet, and image processing toolboxes for Matlab \
are also required.\" Since we are only interested in the unmixing portion \
now, only the base MATLAB system and the image processing toolboxes are \
discussed.\
\>", "Text",
CellChangeTimes->{{3.4742909147644205`*^9, 3.4742910449577527`*^9}, {
3.474291145179437*^9, 3.474291145179437*^9}, {3.474300866806733*^9,
3.4743008799283733`*^9}},
TextJustification->0.,ExpressionUUID->"5bd6001e-e4f5-4a7b-830a-afa72ea67f10"]
}, Open ]],
Cell[CellGroupData[{
Cell["Structure of the Program", "Section",
CellChangeTimes->{{3.4742911464605646`*^9, 3.4742911513663435`*^9}, {
3.4742916172329397`*^9, 3.4742916172329397`*^9}},
TextAlignment->Left,
FontFamily->"Calibri",
FontSize->16,ExpressionUUID->"92d0f821-9b7e-4e5a-811c-6d721322ec9f"],
Cell["\<\
The program is well structured and easily understood. At the top level are \
three functions, the first of which will download the images into the correct \
location for further processing. Since this is done only once it is not \
discussed further here. A second top-level function will generate all of the \
tables and figures once processing has been completed (see next.) That is not \
of interest now. The third function processes the image files. That is where \
our interest lies.\
\>", "Text",
CellChangeTimes->{{3.4742916199512725`*^9,
3.4742917954079075`*^9}},ExpressionUUID->"c98e1b51-e8af-479b-9ebb-\
5a6223b52319"],
Cell["\<\
processData\[LineSeparator]\tsetup(dataroot)\[LineSeparator]\toptions = \
setOptions(inputs)
\toutputs = prepareData(method, params)
\thandleData(data, params)
\t\tprocessImage(readpath, writepath, params)
\t\t\tH = findH(V, W)
\t\t\t[W, H] = findWH(V, inputs)
\t\t\t[J, ma] = reconIH(I, H, IDX, sc)\
\>", "Program",
CellChangeTimes->{
3.474633980100915*^9},ExpressionUUID->"e09cea28-5c39-4dec-86b2-\
9ccc99f98cd8"],
Cell["\<\
In a translation, it is often easier to work \"from the inside out\" and so \
we shall here.\
\>", "Text",
CellChangeTimes->{{3.474292244644294*^9, 3.474292245347297*^9}, {
3.474292298260027*^9,
3.474292327036304*^9}},ExpressionUUID->"bc98d50b-0859-43c8-a508-\
b5dc897283a9"]
}, Open ]],
Cell[CellGroupData[{
Cell["The Functions", "Section",
CellChangeTimes->{{3.474292333769515*^9, 3.474292427518339*^9}, {
3.474292560041461*^9, 3.4742926449549675`*^9}, {3.4742928104589405`*^9,
3.4742928535496564`*^9}, {3.474292971865424*^9, 3.4742929747713394`*^9}, {
3.4743035019971533`*^9, 3.4743035692531805`*^9}, {3.4743036029671993`*^9,
3.474303610254181*^9}, {3.47430416564487*^9, 3.474304193881138*^9}, {
3.4743042759789476`*^9, 3.4743043061070147`*^9}, {3.4743043517285776`*^9,
3.4743043531044135`*^9}, {3.4743773111095066`*^9,
3.474377347359042*^9}},ExpressionUUID->"3f91b966-5137-41bd-b9f6-\
511de14214fc"],
Cell[CellGroupData[{
Cell[TextData[{
StyleBox["Mathematica",
FontSlant->"Italic"],
" versions of some MATLAB functions"
}], "Subsection",
CellChangeTimes->{{3.4743773886241393`*^9, 3.4743773992177534`*^9}, {
3.4743774971852493`*^9, 3.4743774972008743`*^9}, {3.474387562382146*^9,
3.4743875789284916`*^9}, {3.4743877659537563`*^9, 3.4743877659537563`*^9}, {
3.474637579462118*^9,
3.4746375919620385`*^9}},ExpressionUUID->"73c6aaee-1975-44d4-b4c5-\
d33db1123455"],
Cell["\<\
The software for the original paper is written in MATLAB 7.1. Since there is \
not a one-to-one correspondence between the syntax and built-in functions \
available in the two languages, some bridging must be done.\
\>", "Text",
CellChangeTimes->{{3.474637623430587*^9, 3.4746376858520293`*^9}, {
3.474637732960801*^9, 3.474637749085595*^9}, {3.4746378924431353`*^9,
3.4746378924431353`*^9}, {3.474640437989501*^9, 3.474640437989501*^9},
3.811521957439865*^9},ExpressionUUID->"ee56861b-0844-4ea5-8888-\
0fb91882432a"],
Cell[CellGroupData[{
Cell["single()", "Subsubsection",
CellChangeTimes->{{3.474640440085757*^9, 3.474640442056863*^9}},
TextAlignment->Left,ExpressionUUID->"4c64ccac-7aae-406a-8021-7644a895ed8d"],
Cell[TextData[{
"This function is used to convert its arguments to single-precision floating \
point, presumably to reduce memory requirements. We will simply allow ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" to use whatever numerical representation it believes appropriate and will \
not do explicit casts to single precision."
}], "Item1Paragraph",
CellChangeTimes->{{3.4746378947556057`*^9, 3.4746379843950357`*^9}, {
3.474638109204341*^9, 3.474638122047762*^9}, {3.4746384467328334`*^9,
3.4746384467328334`*^9}, {3.4746406098769836`*^9, 3.4746406098769836`*^9},
3.474647807863309*^9},ExpressionUUID->"8a976a71-b41d-452d-9026-\
c0cd2758bc5b"]
}, Open ]],
Cell[CellGroupData[{
Cell["reshape()", "Subsubsection",
CellChangeTimes->{{3.474640611846843*^9,
3.4746406140981097`*^9}},ExpressionUUID->"eb460d5b-acea-428b-ade5-\
c7169bf40b4a"],
Cell[CellGroupData[{
Cell["\<\
This function does general matrix reshaping. However, in the calculation \
chain described above, the function is invariably used to convert image data \
into an column matrix of tuples. The dimensionality of the tuples is the \
number of color planes in the original image. Further, the elements are \
almost always subtracted from 255 and duplicate rows (tuples) are eliminated.\
\>", "Item1Paragraph",
CellChangeTimes->{{3.4746384479659758`*^9, 3.4746384925620317`*^9}, {
3.474638524998365*^9, 3.4746386454655643`*^9}, {3.4746386802553387`*^9,
3.4746387280678563`*^9}, {3.4746390961118426`*^9, 3.4746391355133877`*^9}, {
3.4746392394947023`*^9, 3.4746392398539047`*^9}, {3.474640227240136*^9,
3.474640243092304*^9}, {3.474647811409594*^9,
3.4746478146278086`*^9}},ExpressionUUID->"dcaca5cc-7e82-4ba6-b920-\
cc5b6fa4e0a5"],
Cell["\<\
Creating a function to do what is desired, not just trying to reproduce the \
MATLAB function, is straightforward. Let's start with a small image to work \
with. (Note: You will probably need to change the following file path to \
match the directory layout on your system.)\
\>", "Item1Paragraph",
CellChangeTimes->{{3.4746384479659758`*^9, 3.4746384925620317`*^9}, {
3.474638524998365*^9, 3.4746386454655643`*^9}, {3.4746386802553387`*^9,
3.4746387280678563`*^9}, {3.4746390961118426`*^9,
3.4746391355133877`*^9}, {3.4746392394947023`*^9, 3.474639330185525*^9}, {
3.4746393928117*^9, 3.4746393928117*^9}, 3.474640252775402*^9, {
3.5250115287105083`*^9,
3.5250115605813646`*^9}},ExpressionUUID->"78e0514c-dd10-4c62-be15-\
fc6f43dd2a85"]
}, Open ]],
Cell[BoxData[
RowBox[{
RowBox[{
"smlImgPath", "=",
"\"\<~/projects/Color-Deconvolution/Images/Small Test.PNG\>\""}],
";"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, 3.4746394849556875`*^9, {
3.525011486356434*^9, 3.5250115075100713`*^9}, {3.811522110880382*^9,
3.811522152428301*^9}},
CellLabel->
"In[709]:=",ExpressionUUID->"f07397f5-b1c9-4e14-9a9e-98ffcfec77a5"],
Cell[CellGroupData[{
Cell[BoxData[
StyleBox[Cell[BoxData[
RowBox[{"smlImg", " ", "=", " ",
RowBox[{"Import", "[", "smlImgPath", "]"}]}]], "Input",
CellChangeTimes->{{3.4743993774500003`*^9, 3.4743993947451286`*^9}},
ExpressionUUID->"bdda4521-515b-4352-8cdf-1c95a8a8aedb"], "Input"]], "Input",
CellChangeTimes->{3.4746393940454826`*^9, 3.4746394391958265`*^9,
3.4746394849556875`*^9},
CellLabel->
"In[710]:=",ExpressionUUID->"68e83bdb-117b-4ec9-bb73-0037ff8bb46a"],
Cell[BoxData[
InterpretationBox[Cell[BoxData[
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UBAkIQzEIPZ/hgY8qKHhPxo6AAOjGsnUCAYka0QCdNJItlOHVnQMMo0A8Eg2
iA==
"], {{0, 9.6}, {11.4, 0}}, {0, 255},
ColorFunction->RGBColor,
ImageResolution->{120, 120}],
BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{11.4, 9.6},
PlotRange->{{0, 11.4}, {0, 9.6}}]], "Input",
CellChangeTimes->{{3.4743993774500003`*^9, 3.4743993947451286`*^9}},
ExpressionUUID->"5aa88632-cdc1-42c6-bd6c-ebe9bf168225"],
ExpressionCell[
Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UBAkIQzEIPZ/hgY8qKHhPxo6AAOjGsnUCAYka0QCdNJItlOHVnQMMo0A8Eg2
iA==
"], "Byte", ColorSpace -> "RGB", ImageResolution -> {120, 120},
Interleaving -> True], "Input",
CellChangeTimes -> {{3.4743993774500003`*^9,
3.4743993947451286`*^9}}]]], "Output",
CellChangeTimes->{
3.474639455079027*^9, 3.4746394994020596`*^9, 3.4747348847050643`*^9,
3.4748020195724945`*^9, 3.4748037288986444`*^9, 3.474803774882454*^9,
3.4748054317076397`*^9, 3.474805654342162*^9, {3.4748075988620005`*^9,
3.4748076182871847`*^9}, 3.474808263172038*^9, 3.4748083143893623`*^9,
3.474809794538604*^9, 3.4748141362664795`*^9, 3.474814521083828*^9,
3.47481459736014*^9, {3.474815203098461*^9, 3.4748152283304462`*^9},
3.474820847574823*^9, 3.4748215328225355`*^9, {3.4748295868207026`*^9,
3.4748296348431168`*^9}, 3.474830357581381*^9, 3.4748304681463485`*^9,
3.474830564541522*^9, 3.474832130526839*^9, 3.4749012818212385`*^9,
3.4749016030355515`*^9, 3.474912024485411*^9, 3.474916444935841*^9,
3.474917011488313*^9, 3.4749741949296427`*^9, 3.4749784938935394`*^9,
3.4749872413027663`*^9, 3.4749881819651785`*^9, 3.4749990165076666`*^9,
3.474999471429824*^9, 3.4749996895282116`*^9, 3.4750007797186966`*^9,
3.4750014448448095`*^9, 3.4750026562758713`*^9, 3.5250117223692484`*^9,
3.811522089940317*^9, 3.811522221161643*^9, 3.811522364844604*^9,
3.8115225858658447`*^9, 3.811522737743137*^9, 3.811523233028668*^9},
CellLabel->
"Out[710]=",ExpressionUUID->"65191060-b372-4eda-b53e-96ffdd2263ff"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Dimensions", "[",
RowBox[{"Import", "[",
RowBox[{"smlImgPath", ",", " ", "\"\<Data\>\""}], "]"}], "]"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396036502075`*^9}},
CellLabel->
"In[711]:=",ExpressionUUID->"57a64c64-0a4c-44ab-8bfd-5856b30c04a6"],
Cell[BoxData[
RowBox[{"{",
RowBox[{"16", ",", "19", ",", "3"}], "}"}]], "Output",
CellChangeTimes->{
3.474639622047858*^9, 3.474734884783104*^9, 3.474802019634591*^9,
3.4748037289454556`*^9, 3.474803774929265*^9, 3.4748054317544746`*^9,
3.4748056544202194`*^9, {3.4748075989244604`*^9, 3.47480761833403*^9},
3.4748082632188973`*^9, 3.4748083144362216`*^9, 3.4748097945854654`*^9,
3.474814136313346*^9, 3.474814521146312*^9, 3.474814597407002*^9, {
3.474815203145361*^9, 3.4748152283773456`*^9}, 3.4748208476373205`*^9,
3.4748215328850317`*^9, {3.4748295868673563`*^9, 3.4748296348897705`*^9},
3.4748303576438026`*^9, 3.474830468193165*^9, 3.474830564603944*^9,
3.474832130573663*^9, 3.4749012818837423`*^9, 3.474901603098014*^9,
3.4749120245478954`*^9, 3.4749164449827104`*^9, 3.4749170115351825`*^9,
3.4749741950077333`*^9, 3.474978493940398*^9, 3.4749872413651395`*^9,
3.474988184093524*^9, 3.474999016570137*^9, 3.474999471476677*^9,
3.474999689575064*^9, 3.4750007797812586`*^9, 3.4750014449073515`*^9,
3.475002656322776*^9, 3.525011722587649*^9, 3.8115220899710073`*^9,
3.811522221193445*^9, 3.811522364856175*^9, 3.811522585895212*^9,
3.811522737773489*^9, 3.811523233057213*^9},
CellLabel->
"Out[711]=",ExpressionUUID->"f48f8b23-b6e2-4325-a34a-ffebd06969ba"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Import", "[",
RowBox[{"smlImgPath", ",", " ", "\"\<BitDepth\>\""}], "]"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}},
CellLabel->
"In[712]:=",ExpressionUUID->"b2570d32-3202-43e0-b92b-0e0051f8aac6"],
Cell[BoxData["8"], "Output",
CellChangeTimes->{
3.474639657031506*^9, 3.474734884845535*^9, 3.4748020196966887`*^9,
3.4748037289922667`*^9, 3.4748037749760756`*^9, 3.4748054318013096`*^9,
3.4748056544670534`*^9, {3.474807598971306*^9, 3.47480761839649*^9},
3.4748082632657566`*^9, 3.474808314483081*^9, 3.474809794632327*^9,
3.474814136360213*^9, 3.4748145211931753`*^9, 3.474814597453864*^9, {
3.4748152031922607`*^9, 3.474815228424245*^9}, 3.474820847684194*^9,
3.4748215329319043`*^9, {3.4748295869295616`*^9, 3.474829634951976*^9},
3.4748303576906195`*^9, 3.4748304682399817`*^9, 3.4748305646507607`*^9,
3.4748321306360955`*^9, 3.474901281914994*^9, 3.474901603129245*^9,
3.474912024594758*^9, 3.474916445029579*^9, 3.4749170115820513`*^9,
3.4749741950545874`*^9, 3.474978493987256*^9, 3.4749872414119196`*^9,
3.4749881857993307`*^9, 3.474999016616989*^9, 3.474999471523529*^9,
3.474999689621917*^9, 3.4750007798281803`*^9, 3.4750014449386225`*^9,
3.47500265636968*^9, 3.525011722650049*^9, 3.811522089999866*^9,
3.811522221222529*^9, 3.811522364889503*^9, 3.811522585924863*^9,
3.811522737802709*^9, 3.811523233087137*^9},
CellLabel->
"Out[712]=",ExpressionUUID->"230b5d7f-0b11-4192-b7cf-2b9ec31ab429"]
}, Open ]],
Cell["\<\
This shows that this is an image with 16 rows, 19 columns, and three color \
channels and that the data consists of 8-bit values. From the image, it is \
obvious that there should be several duplicate rows in the reshaped matrix. \
The data looks like:\
\>", "Item1Paragraph",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9,
3.474639676756661*^9}},ExpressionUUID->"959a967a-d33e-4475-b0d7-\
e07802fabe7c"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Short", "[",
RowBox[{
RowBox[{"ImageData", "[",
RowBox[{"smlImg", ",", " ", "\"\<Byte\>\""}], "]"}], ",", " ", "3"}],
"]"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9, 3.4746396935925417`*^9},
3.47463973933694*^9},
CellLabel->
"In[713]:=",ExpressionUUID->"8eecc34e-ec97-44cc-b74c-929cbfb3c811"],
Cell[BoxData[
TagBox[
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"\[LeftSkeleton]", "8", "\[RightSkeleton]"}], ",",
RowBox[{"{",
RowBox[{"128", ",", "128", ",", "255"}], "}"}], ",",
RowBox[{"{",
RowBox[{"128", ",", "128", ",", "255"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}]}], "}"}], ",",
RowBox[{"\[LeftSkeleton]", "14", "\[RightSkeleton]"}], ",",
RowBox[{"{",
RowBox[{"\[LeftSkeleton]", "1", "\[RightSkeleton]"}], "}"}]}], "}"}],
Short[#, 3]& ]], "Output",
CellChangeTimes->{
3.474639745459157*^9, 3.4747348848767505`*^9, 3.4748020197122126`*^9,
3.4748037290234737`*^9, 3.4748037750072827`*^9, 3.474805431816921*^9,
3.474805654482665*^9, {3.4748075989869213`*^9, 3.47480761842772*^9},
3.474808263296996*^9, 3.4748083145143204`*^9, 3.474809794663568*^9,
3.474814136391457*^9, 3.4748145212244177`*^9, 3.4748145974851055`*^9, {
3.474815203223527*^9, 3.4748152284555116`*^9}, 3.4748208476998186`*^9,
3.4748215329475284`*^9, {3.474829586945113*^9, 3.4748296349675274`*^9},
3.4748303577218304`*^9, 3.4748304682711926`*^9, 3.4748305646819715`*^9,
3.474832130651704*^9, 3.4749012819462457`*^9, 3.4749016031604757`*^9,
3.474912024610379*^9, 3.4749164450608253`*^9, 3.4749170116132975`*^9,
3.4749741950858235`*^9, 3.4749784940184946`*^9, 3.474987241427513*^9,
3.474988190447262*^9, 3.474999016648224*^9, 3.4749994715547643`*^9,
3.4749996896531515`*^9, 3.4750007798438206`*^9, 3.4750014449698935`*^9,
3.4750026564009495`*^9, 3.525011722696849*^9, 3.811522090010597*^9,
3.811522221229187*^9, 3.8115223648962297`*^9, 3.81152258593196*^9,
3.811522737809354*^9, 3.811523233093688*^9},
CellLabel->
"Out[713]//Short=",ExpressionUUID->"cecdcff6-97dd-4d98-8c4b-c46ab5435afc"]
}, Open ]],
Cell[TextData[{
"That is, it is rows of RGB triples. We can obtain the tall matrix form \
generated by the ",
StyleBox["reshape()", "Code"],
" function by flattening the list by one level:"
}], "Item1Paragraph",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9,
3.4746396935925417`*^9}, {3.474639788798829*^9, 3.4746397921722956`*^9},
3.4746398292648077`*^9},ExpressionUUID->"a31316e4-62c7-4393-b381-\
df7a58a1134e"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"Short", "[",
RowBox[{
RowBox[{"Flatten", "[",
RowBox[{
RowBox[{"ImageData", "[",
RowBox[{"smlImg", ",", " ", "\"\<Byte\>\""}], "]"}], ",", " ", "1"}],
"]"}], ",", " ", "3"}], "]"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9,
3.4746396935925417`*^9}, {3.474639788798829*^9, 3.4746397921722956`*^9}, {
3.4746398292648077`*^9, 3.47463983743297*^9}},
CellLabel->
"In[714]:=",ExpressionUUID->"cc6ccf75-02ac-4b3a-b551-1afc64b8afff"],
Cell[BoxData[
TagBox[
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"{",
RowBox[{"255", ",", "0", ",", "128"}], "}"}], ",",
RowBox[{"\[LeftSkeleton]", "293", "\[RightSkeleton]"}], ",",
RowBox[{"{",
RowBox[{"128", ",", "128", ",", "255"}], "}"}], ",",
RowBox[{"{",
RowBox[{"128", ",", "128", ",", "255"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}], ",",
RowBox[{"{",
RowBox[{"192", ",", "192", ",", "192"}], "}"}]}], "}"}],
Short[#, 3]& ]], "Output",
CellChangeTimes->{
3.4746398574551177`*^9, 3.4747348849079657`*^9, 3.4748020197587857`*^9,
3.474803729054681*^9, 3.4748037750384903`*^9, 3.4748054318481445`*^9,
3.4748056545138874`*^9, {3.4748075990181513`*^9, 3.4748076184433355`*^9},
3.474808263328236*^9, 3.47480831454556*^9, 3.474809794694809*^9,
3.474814136422702*^9, 3.474814521240039*^9, 3.474814597516347*^9, {
3.474815203254793*^9, 3.4748152284867783`*^9}, 3.4748208477310677`*^9,
3.474821532978777*^9, {3.4748295869762154`*^9, 3.47482963499863*^9},
3.4748303577530413`*^9, 3.4748304683024035`*^9, 3.4748305646975765`*^9,
3.4748321306829195`*^9, 3.474901281977497*^9, 3.4749016031917067`*^9,
3.474912024641621*^9, 3.4749164450920715`*^9, 3.4749170116289206`*^9,
3.474974195132678*^9, 3.4749784940497336`*^9, 3.4749872414586997`*^9,
3.47498819323289*^9, 3.474999016679459*^9, 3.4749994715859995`*^9,
3.4749996896843867`*^9, 3.475000779875102*^9, 3.475001444985529*^9,
3.4750026564322195`*^9, 3.5250117227436495`*^9, 3.811522090037437*^9,
3.8115222212357407`*^9, 3.8115223649277763`*^9, 3.811522585961124*^9,
3.811522737837731*^9, 3.811523233119639*^9},
CellLabel->
"Out[714]//Short=",ExpressionUUID->"78f51f98-e9fa-4621-9263-4efbf70fcf1f"]
}, Open ]],
Cell["\<\
And we can subtract each byte from 255 and reduce the matrix to the \
non-duplicated rows like this:\
\>", "Item1Paragraph",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9,
3.4746396935925417`*^9}, {3.474639788798829*^9, 3.4746397921722956`*^9}, {
3.4746398292648077`*^9, 3.47463983743297*^9}, {3.4746398751345806`*^9,
3.474639903184329*^9}, {3.474639952162063*^9,
3.4746399622512264`*^9}},ExpressionUUID->"97d0726c-4130-4ff1-b150-\
a7fe6cc0406e"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"MatrixForm", "[",
RowBox[{"DeleteDuplicates", "[",
RowBox[{"255", "-",
RowBox[{"Flatten", "[",
RowBox[{
RowBox[{"ImageData", "[",
RowBox[{"smlImg", ",", " ", "\"\<Byte\>\""}], "]"}], ",", " ", "1"}],
"]"}]}], "]"}], "]"}]], "Input",
CellChangeTimes->{
3.4746393940454826`*^9, 3.4746394391958265`*^9, {3.474639593451849*^9,
3.4746396407110095`*^9}, {3.4746396711655245`*^9,
3.4746396935925417`*^9}, {3.474639788798829*^9, 3.4746397921722956`*^9}, {
3.4746398292648077`*^9, 3.47463983743297*^9}, {3.4746398751345806`*^9,
3.4746399200360427`*^9}, {3.4746399703725348`*^9, 3.474639971543877*^9},
3.4748304563485904`*^9, {3.4748305598130555`*^9, 3.4748305609990735`*^9}},
CellLabel->
"In[715]:=",ExpressionUUID->"790d316e-cefd-4057-adb3-6dd7d760e74d"],
Cell[BoxData[
TagBox[
RowBox[{"(", "\[NoBreak]", GridBox[{
{"0", "255", "127"},
{"127", "127", "0"},
{"63", "63", "63"},
{"0", "0", "0"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]},
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}}], "\[NoBreak]", ")"}],
Function[BoxForm`e$,
MatrixForm[BoxForm`e$]]]], "Output",
CellChangeTimes->{
3.4746399277044315`*^9, 3.474639974230156*^9, 3.474734884970397*^9,
3.474802019836407*^9, 3.474803729085888*^9, 3.4748037750696974`*^9,
3.474805431879368*^9, 3.4748056545451107`*^9, {3.4748075990493813`*^9,
3.4748076184745655`*^9}, 3.474808263343856*^9, 3.4748083145768*^9,
3.47480979472605*^9, 3.474814136453946*^9, 3.4748145212712812`*^9,
3.4748145975319676`*^9, {3.4748152032860594`*^9, 3.4748152285180445`*^9},
3.4748208477623167`*^9, 3.474821533010025*^9, {3.4748295870073185`*^9,
3.4748296350297327`*^9}, 3.474830357784252*^9, 3.4748304683336143`*^9,
3.474830564728788*^9, 3.4748321306985283`*^9, 3.474901282008749*^9,
3.4749016032229376`*^9, 3.474912024672863*^9, 3.4749164451076946`*^9,
3.4749170116601667`*^9, 3.474974195163914*^9, 3.4749784940809727`*^9,
3.4749872415054793`*^9, 3.474988196284562*^9, 3.4749990167106943`*^9,
3.474999471617234*^9, 3.474999689715622*^9, 3.4750007799063826`*^9,
3.4750014450168*^9, 3.475002656447854*^9, 3.525011722790449*^9,
3.8115220900479717`*^9, 3.811522221267497*^9, 3.81152236493401*^9,
3.81152258596841*^9, 3.8115227378441133`*^9, 3.8115232331260233`*^9},
CellLabel->
"Out[715]//MatrixForm=",ExpressionUUID->"a0a6eeb2-61fb-4974-9746-\
0e96817ccf3a"]
}, Open ]],
Cell[TextData[{
"As expected, there are only four unique triples since there are only four \
unique colors in the image. So, such a ",
StyleBox["findUniqueRows()", "Code"],
" function might be written as:"
}], "Item1Paragraph",
CellChangeTimes->{{3.4746384479659758`*^9, 3.4746384925620317`*^9}, {
3.474638524998365*^9, 3.4746386454655643`*^9}, {3.4746386802553387`*^9,
3.4746387280678563`*^9}, {3.4746390961118426`*^9, 3.4746391355133877`*^9}, {
3.4746392394947023`*^9, 3.474639330185525*^9}, {3.4746399975945344`*^9,
3.4746400290021315`*^9}, {3.4746400761994247`*^9, 3.474640076808523*^9}, {
3.474651327886855*^9,
3.4746513646463623`*^9}},ExpressionUUID->"edbe7622-0ab9-4e9f-a979-\
42fc49c7edb3"],
Cell[BoxData[
RowBox[{
RowBox[{"findUniqueRows", "[", "img_", "]"}], " ", ":=", " ",
RowBox[{"DeleteDuplicates", "[",
RowBox[{"Flatten", "[",
RowBox[{
RowBox[{"255", "-",
RowBox[{"ImageData", "[",
RowBox[{"img", ",", " ", "\"\<Byte\>\""}], "]"}]}], ",", "1"}], "]"}],
"]"}]}]], "Input",
CellChangeTimes->{
3.4746390905991125`*^9, {3.4746391254245763`*^9, 3.4746391302034373`*^9}, {
3.4746400325786304`*^9, 3.4746400351087303`*^9}, {3.474640071279786*^9,
3.474640072091917*^9}, 3.474830461701277*^9, {3.4748305554591208`*^9,
3.4748305565983224`*^9}},
CellLabel->
"In[716]:=",ExpressionUUID->"34d65b76-b33b-40e3-a0c4-5ec6c1b0c0be"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"findUniqueRows", "[", "smlImg", "]"}], "//",
"MatrixForm"}]], "Input",
CellChangeTimes->{{3.474640062408819*^9, 3.4746400915986743`*^9}},
CellLabel->
"In[717]:=",ExpressionUUID->"5be19611-674a-4030-a4db-7f852fc05103"],
Cell[BoxData[
TagBox[
RowBox[{"(", "\[NoBreak]", GridBox[{
{"0", "255", "127"},
{"127", "127", "0"},
{"63", "63", "63"},
{"0", "0", "0"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]},
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}}], "\[NoBreak]", ")"}],
Function[BoxForm`e$,
MatrixForm[BoxForm`e$]]]], "Output",
CellChangeTimes->{
3.4746407419825935`*^9, 3.4747348850172205`*^9, 3.4748020198829803`*^9,
3.474803729117095*^9, 3.4748037751009045`*^9, 3.474805431910591*^9,
3.474805654576333*^9, {3.4748075990806117`*^9, 3.474807618521411*^9},
3.474808263390715*^9, 3.4748083146080394`*^9, 3.4748097947729115`*^9,
3.4748141364851904`*^9, 3.474814521302523*^9, 3.47481459757883*^9, {
3.474815203317326*^9, 3.4748152285493107`*^9}, 3.4748208477935658`*^9,
3.474821533041273*^9, {3.4748295870539722`*^9, 3.4748296350763865`*^9},
3.474830357815463*^9, 3.4748304683648252`*^9, 3.4748305647756042`*^9,
3.474832130729744*^9, 3.474901282040001*^9, 3.4749016032541685`*^9,
3.4749120247041054`*^9, 3.4749164451389403`*^9, 3.4749170116914124`*^9,
3.4749741952576227`*^9, 3.474978494112211*^9, 3.474987241536666*^9,
3.4749882017775717`*^9, 3.474999016741929*^9, 3.474999471648469*^9,
3.4749996897468567`*^9, 3.475000779937664*^9, 3.475001445048071*^9,
3.4750026564791236`*^9, 3.5250117228372493`*^9, 3.81152209007934*^9,
3.811522221295927*^9, 3.811522364944682*^9, 3.811522586000422*^9,
3.81152273788135*^9, 3.811523233155685*^9},
CellLabel->
"Out[717]//MatrixForm=",ExpressionUUID->"3fdbe083-00fc-49c6-8654-\
b65a5e0ce81a"]
}, Open ]]
}, Open ]],
Cell[CellGroupData[{
Cell["unique()", "Subsubsection",
CellChangeTimes->{{3.4746406599989467`*^9, 3.474640677962183*^9}, {
3.4746414934438095`*^9,
3.4746414934438095`*^9}},ExpressionUUID->"668af8e1-10ed-41e8-8e9b-\
cf5ac7da32a3"],
Cell[TextData[{
"This function is used to find the unique items in a collection. It has \
several options and several outputs. From the MATLAB web page at ",
ButtonBox["http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.\
html",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.\
html"], None},
ButtonNote->
"http://www.mathworks.com/access/helpdesk/help/techdoc/ref/unique.html"],
" (edited to use less space):"
}], "Item1Paragraph",
CellChangeTimes->{{3.4746407069003468`*^9, 3.4746407074631634`*^9}, {
3.4746517355846276`*^9, 3.474651775078055*^9}, {3.474651873249216*^9,
3.4746519628905478`*^9}, {3.4746526938157763`*^9,
3.474652696565319*^9}},ExpressionUUID->"353e9ec4-4b46-4e03-9b4f-\
a71ed8072645"],
Cell[TextData[{
StyleBox["b = unique(A)\nb = unique(A, 'rows')\n[b, m, n] = unique(...)\n[b, \
m, n] = unique(..., occurrence)\n\n", "Program"],
StyleBox["Description", "Program",
FontFamily->"Constantia"],
StyleBox["\n\nb = unique(A) ", "Program"],
StyleBox["returns the same values as in A but with no repetitions. A can be \
a numeric or character array or a cell array of strings. If A is a vector or \
an array, b is a vector of unique values from A. If A is a cell array of \
strings, b is a cell vector of unique strings from A. The resulting vector b \
is sorted in ascending order and its elements are of the same class as A.",
"Program",
FontFamily->"Constantia"],
StyleBox["\n\nb = unique(A, 'rows') ", "Program"],
StyleBox["returns the unique rows of A.", "Program",
FontFamily->"Constantia"],
StyleBox["\n\n[b, m, n] = unique(...) ", "Program"],
StyleBox["also returns index vectors m and n such that b = A(m) and A = \
b(n). Each element of m is the greatest subscript such that b = A(m). For row \
combinations, b = A(m,:) and A = b(n,:).", "Program",
FontFamily->"Constantia"],
StyleBox["\n\n[b, m, n] = unique(..., occurrence)", "Program"],
StyleBox[", where occurrence can be", "Program",
FontFamily->"Constantia"],
StyleBox["\n\n ", "Program"],
StyleBox["* 'first', which returns the vector m to index the first \
occurrence of each unique value in A, or\n * 'last', which returns the \
vector m to index the last occurrence.\n\nIf you do not specify occurrence, \
it defaults to 'last'.\n\nYou can specify 'rows' in the same command as \
'first' or 'last'. The order of appearance in the argument list is not \
important.\nExamples", "Program",
FontFamily->"Constantia"],
StyleBox["\n\nA = [1 1 5 6 2 3 3 9 8 6 2 4] \nA = 1 1 5 6 2 3 3 \
9 8 6 2 4\n\n", "Program"],
StyleBox["Get a sorted vector of unique elements of A. Also get indices of \
the first elements in A that make up vector b, and the first elements in b \
that make up vector A:", "Program",
FontFamily->"Constantia"],
StyleBox["\n\n[b1, m1, n1] = unique(A, 'first') \nb1 = 1 2 3 4 5 6 \
8 9 \nm1 = 1 5 6 12 3 4 9 8 \nn1 = 1 1 5 6 2 3 3 \
8 7 6 2 4\n\n", "Program"],
StyleBox["Verify that b1 = A(m1) and A = b1(n1):", "Program",
FontFamily->"Constantia"],
StyleBox["\n\nall(b1 == A(m1)) && all(A == b1(n1)) \nans = 1\n\n", "Program"],
StyleBox["Get a sorted vector of unique elements of A. Also get indices of \
the last elements in A that make up vector b, and the last elements in b that \
make up vector A:", "Program",
FontFamily->"Constantia"],
StyleBox["\n\n[b2, m2, n2] = unique(A, 'last') \nb2 = 1 2 3 4 5 6 \
8 9 \nm2 = 2 11 7 12 3 10 9 8 \nn2 = 1 1 5 6 2 3 3 \
8 7 6 2 4\n\n", "Program"],
StyleBox["Verify that b2 = A(m2) and A = b2(n2):", "Program",
FontFamily->"Constantia"],
StyleBox["\n\nall(b2 == A(m2)) && all(A == b2(n2)) \nans = 1\n\n", "Program"],
StyleBox["Because NaNs are not equal to each other, unique treats them as \
unique elements.", "Program",
FontFamily->"Constantia"],
StyleBox["\n\nunique([1 1 NaN NaN]) \nans = 1 NaN NaN", "Program"]
}], "Program",
CellChangeTimes->{{3.4746407069003468`*^9, 3.4746407074631634`*^9}, {
3.4746517355846276`*^9, 3.474651775078055*^9}, {3.474651873249216*^9,
3.4746519315832577`*^9}, {3.4746519888081093`*^9, 3.4746520902912197`*^9}, {
3.474652220472679*^9,
3.474652222972263*^9}},ExpressionUUID->"ae361b7a-d9d7-48fc-bb7a-\
a1525f112837"],
Cell[CellGroupData[{
Cell[TextData[{
"In the MATLAB code, the ",
StyleBox["unique()", "Code",
FontFamily->"Consolas",
FontWeight->"Plain"],
" function is used to generate a set of indices that map from the image \
pixels to the unmixed image ",
StyleBox["via",
FontSlant->"Italic"],
" the H matrix."
}], "Item1Paragraph",
CellChangeTimes->{{3.4743722261655645`*^9, 3.47437224418511*^9}, {
3.4743722899138083`*^9, 3.4743723296724577`*^9}, {3.474372435881908*^9,
3.474372483716155*^9}, {3.4743728194441547`*^9, 3.4743728740273924`*^9}, {
3.474377118020821*^9,
3.474377118020821*^9}},ExpressionUUID->"ef9522e8-ef50-487e-ab85-\
fa2986842a0f"],
Cell[TextData[{
StyleBox["Mathematica",
FontSlant->"Italic"],
" does not have a function built in that provides identical functionality. \
However, such a function can be constructed easily. (This implementation \
comes from \"Timo\" in answer to a question on Stack Overflow at ",
ButtonBox["http://stackoverflow.com/questions/2203737/does-mathematica-have-\
a-function-equivalent-to-matlabs-unique-function",
BaseStyle->"Hyperlink",
ButtonData->{
URL["http://stackoverflow.com/questions/2203737/does-mathematica-have-a-\
function-equivalent-to-matlabs-unique-function"], None},
ButtonNote->
"http://stackoverflow.com/questions/2203737/does-mathematica-have-a-\
function-equivalent-to-matlabs-unique-function"],
"."
}], "Item1Paragraph",
CellChangeTimes->{{3.4743722261655645`*^9, 3.47437224418511*^9}, {
3.4743722899138083`*^9, 3.4743723296724577`*^9}, {3.474372435881908*^9,
3.474372483716155*^9}, {3.4743728194441547`*^9, 3.4743728740273924`*^9}, {
3.474377118020821*^9, 3.474377120911354*^9}, {3.474652722232922*^9,
3.474652730403437*^9}},ExpressionUUID->"720ede05-0f65-40f2-be2b-\
70a24199bfb1"]
}, Open ]],
Cell["\<\
You can easily build similar functionality yourself with Mathematica's \
Position[]. E.g. given a list of numbers you could do the following:
In[1] := A = {1, 5, 2, 3, 7, 3, 2, 8, 6, 5, 9, 2, 1};
In[2] := {#, Flatten[Position[A, #]]} & /@ Union[A]
Out[2]:= {{1, {1, 13}}, {2, {3, 7, 12}}, {3, {4, 6}}, {5, {2, 10}}, {6, {9}}, \
{7, {5}}, {8, {8}}, {9, {11}}}
to get the list of unique elements and the indices of where they appear in \
the original list. To replicate exactly the functionality of Matlab's \
Unique(), especially for
[b,m,n] = unique(A)
you need
b = Union[A];
m = Last[Position[A, #]] & /@ b // Flatten;
n = Position[b, #] & /@ A // Flatten;
which now provide the desired behavior
In[1] := A[[#]] & /@ m == b
Out[1]:= True
In[2] := b[[#]] & /@ n == A
Out[2]:= True\
\>", "Program",
CellChangeTimes->{{3.474387108271678*^9,
3.4743871129121547`*^9}},ExpressionUUID->"cc547938-3f31-493a-82c0-\
0e0350acb099"],
Cell["\<\
If we need the indices that map between the reduced and original matrix, we \
can define the function: \
\>", "Item1Paragraph",
CellChangeTimes->{{3.4744085938755827`*^9, 3.4744086273688416`*^9}, {
3.474408982161265*^9, 3.4744089864980726`*^9}, {3.4744093304318767`*^9,
3.474409335782686*^9}, {3.474804292268394*^9,
3.4748042951745324`*^9}},ExpressionUUID->"8d705f98-13d1-4f1b-8509-\
5ee88cdb9d8f"],
Cell[BoxData[
RowBox[{
RowBox[{"uniqueLast", "[", "a_", "]"}], " ", ":=", " ",
RowBox[{"Module", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{", " ", "tmp", " ", "}"}], ",", "\[IndentingNewLine]",
RowBox[{"Return", "[",
RowBox[{"{",
RowBox[{
RowBox[{"tmp", " ", "=", " ",
RowBox[{"Union", "[", "a", "]"}]}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"Last", "[",
RowBox[{"Position", "[",
RowBox[{"a", ",", " ", "#"}], "]"}], "]"}], " ", "&"}], " ", "/@",
" ", "tmp"}], " ", "//", "Flatten"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
RowBox[{"Position", "[",
RowBox[{"tmp", ",", " ", "#"}], "]"}], " ", "&"}], " ", "/@", " ",
"a"}], " ", "//", "Flatten"}]}], "}"}], "]"}]}], "]"}]}]], "Input",
CellChangeTimes->{{3.474374687168406*^9, 3.4743747398382587`*^9}, {
3.474374842676222*^9, 3.4743750327413645`*^9}, {3.4743751985578156`*^9,
3.474375227102539*^9}, {3.474375283426338*^9, 3.474375306971405*^9}, {
3.4743755090808554`*^9, 3.474375545155979*^9}, {3.4743755798093452`*^9,
3.474375581215479*^9}, {3.4746533569085445`*^9, 3.474653359658087*^9}, {
3.4746533955271173`*^9, 3.4746533962613697`*^9}, {3.4748038424665117`*^9,
3.4748038456587243`*^9}, {3.4748039919215155`*^9, 3.4748040083833165`*^9}, {
3.4748042421296945`*^9, 3.4748042431452804`*^9}, {3.474804832218279*^9,
3.4748048353876977`*^9}, {3.474804974698647*^9, 3.4748049754480467`*^9}, {
3.474805539161253*^9, 3.4748055402384396`*^9}},
CellLabel->
"In[718]:=",ExpressionUUID->"2d3f0b44-b051-4e1f-8919-48d815e482dd"],
Cell["\<\
Reproducing the example from the web page with the 'last' occurrence behavior:\
\>", "Item1Paragraph",
CellChangeTimes->{{3.474652959459066*^9, 3.4746529852985153`*^9}, {
3.474659129512944*^9, 3.474659132390686*^9},
3.8115219944086227`*^9},ExpressionUUID->"c2a1e50f-44d6-4227-978a-\
403acca62a46"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"a", "=",
RowBox[{"{",
RowBox[{
"1", ",", "1", ",", "5", ",", "6", ",", "2", ",", "3", ",", "3", ",", "9",
",", "8", ",", "6", ",", "2", ",", "4"}], "}"}]}]], "Input",
CellChangeTimes->{{3.4743108248655577`*^9, 3.474310865280056*^9},
3.474387121693124*^9},
CellLabel->
"In[719]:=",ExpressionUUID->"5b678bc4-0e06-4447-9c2f-e65f459b7133"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
"1", ",", "1", ",", "5", ",", "6", ",", "2", ",", "3", ",", "3", ",", "9",
",", "8", ",", "6", ",", "2", ",", "4"}], "}"}]], "Output",
CellChangeTimes->{
3.474310868973938*^9, 3.474373906050234*^9, 3.474375253819237*^9,
3.4743871325834*^9, 3.474633994744502*^9, 3.474652879206797*^9,
3.4746530587081733`*^9, 3.474653365750823*^9, 3.4746534013542724`*^9,
3.474734885064044*^9, 3.474802019914029*^9, 3.474803729148302*^9,
3.4748037751321115`*^9, 3.4748038522622223`*^9, 3.4748040137193184`*^9,
3.4748042535042577`*^9, 3.474804842304212*^9, 3.474804990732684*^9,
3.474805431941814*^9, 3.47480555083858*^9, 3.4748056546075563`*^9, {
3.474807599111842*^9, 3.474807618552641*^9}, 3.474808263421955*^9,
3.474808314639279*^9, 3.4748097948041525`*^9, 3.474814136532057*^9,
3.4748145213337655`*^9, 3.474814597610071*^9, {3.4748152033485923`*^9,
3.474815228580577*^9}, 3.474820847824815*^9, 3.474821533072521*^9, {
3.474829587085075*^9, 3.474829635107489*^9}, 3.4748303578466744`*^9,
3.474830468396036*^9, 3.474830564806815*^9, 3.4748321307609606`*^9,
3.474901282071253*^9, 3.4749016032853994`*^9, 3.4749120247353473`*^9,
3.474916445170186*^9, 3.474917011722658*^9, 3.474974195288859*^9,
3.4749784941590695`*^9, 3.4749872415678525`*^9, 3.4749882247668343`*^9,
3.4749990167731643`*^9, 3.474999471679704*^9, 3.4749996897937093`*^9,
3.4750007799845853`*^9, 3.475001445079342*^9, 3.475002656510393*^9,
3.5250117228840494`*^9, 3.811522090106176*^9, 3.8115222213125763`*^9,
3.8115223649813833`*^9, 3.81152258603297*^9, 3.811522737918972*^9,
3.811523233185617*^9},
CellLabel->
"Out[719]=",ExpressionUUID->"7d555dc7-00b0-4585-b0af-4c986c595ad8"]
}, Open ]],
Cell[CellGroupData[{