-
Notifications
You must be signed in to change notification settings - Fork 1
/
CELEST_matlab.html
2223 lines (1486 loc) · 70.8 KB
/
CELEST_matlab.html
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
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>CELEST Matlab Tutorial</title><meta name="generator" content="MATLAB 7.14"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2015-06-07"><meta name="DC.source" content="CELEST_matlab.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, tt, code { font-size:12px; }
pre { margin:0px 0px 20px; }
pre.error { color:red; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
</style></head><body><div class="content"><h1>CELEST Matlab Tutorial</h1><!--introduction--><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">Comments, Variables, Vectors and Matrices</a></li><li><a href="#2">Mathematical Operations</a></li><li><a href="#3">Logical operations</a></li><li><a href="#4">Selective Indexing</a></li><li><a href="#5">Control Loops - For and If</a></li><li><a href="#6">Graphics</a></li><li><a href="#7">Functions vs Scripts</a></li><li><a href="#8">Saving and Loading Data</a></li><li><a href="#9">Loading Audio Files</a></li><li><a href="#10">Loading Image Files</a></li><li><a href="#11">Other Useful Built-in Matlab Functions and Miscellany</a></li><li><a href="#12">For more information</a></li></ul></div><h2>Comments, Variables, Vectors and Matrices<a name="1"></a></h2><pre class="codeinput"><span class="comment">% Comments</span>
<span class="comment">% Green text preceded by a percent sign % are comments. Matlab</span>
<span class="comment">% ignores comments -- that is, any text with a percent sign before it will</span>
<span class="comment">% result in Matlab doing nothing. Try it out by copying and pasting the</span>
<span class="comment">% following:</span>
<span class="comment">% a = 5</span>
<span class="comment">% Comments are useful to put in your code so that you remember why are you</span>
<span class="comment">% writing a particular line of code. It also helps other people understand</span>
<span class="comment">% your code should they have to use it.</span>
<span class="comment">% Often it is more useful and clear to assign a value to a variable name.</span>
<span class="comment">% This makes your code more readable and understandable. For example, what</span>
<span class="comment">% is more readable?</span>
<span class="comment">% firingRate * 10</span>
<span class="comment">% OR</span>
<span class="comment">% 5 * 10.</span>
<span class="comment">% Here is how you assign a</span>
<span class="comment">% value to a variable in Matlab:</span>
firingRate = 5
<span class="comment">% You can assign more than one value to a variable. The following code</span>
<span class="comment">% makes a 2 x 1 vector:</span>
b = [3; 4]
<span class="comment">% You can verify this is a 2 x 1 vector by the following command:</span>
size(b)
<span class="comment">% size is a built-in matlab function. It tells you how many elements are</span>
<span class="comment">% in a vector of matrix.</span>
<span class="comment">% You can also make a 2 x 1 vector. Notice that omitting the semi-colon</span>
<span class="comment">% makes this vector "horizontal":</span>
c = [3 4]
size(c)
<span class="comment">% If you don't want to see the output in the command window each time, you</span>
<span class="comment">% can supress the output by ending the statement with a semi-colon:</span>
c = [3 4];
<span class="comment">% You can also make a matrix. Here is a 2x2 matrix:</span>
d = [1 2; 3 4]
d = [1 2; <span class="keyword">...</span>
3 4]
size(d)
<span class="comment">% Typing in each number for a vector or matrix can be a pain in the ass.</span>
<span class="comment">% Luckily, you can tell Matlab to assign a consecutive sequence of numbers</span>
<span class="comment">% to a variable:</span>
vec = 1:10
<span class="comment">% The "step-size" of the consecutive sequence of numbers doesn't have to be</span>
<span class="comment">% one. You can also step by larger or smaller numbers:</span>
vec = 1:.25:10
<span class="comment">% Sometimes you just want to access and change a single elmement of a</span>
<span class="comment">% vector. You can access the fourth element of a vector by:</span>
vec(4)
<span class="comment">% And you can change the fourth element by:</span>
vec(4) = 33
<span class="comment">% Sometimes you don't want to have all the output in the command window.</span>
<span class="comment">% You can clear the command window by typing:</span>
clc
<span class="comment">% Similarly, you might want to clear variables from your workspace to</span>
<span class="comment">% reduce clutter:</span>
clear <span class="string">all</span>
<span class="comment">% A helpful way to learn about built-in matlab functions is to type help</span>
<span class="comment">% before the function:</span>
help <span class="string">clc</span>
help <span class="string">clear</span>
clc
</pre><pre class="codeoutput">
firingRate =
5
b =
3
4
ans =
2 1
c =
3 4
ans =
1 2
d =
1 2
3 4
d =
1 2
3 4
ans =
2 2
vec =
1 2 3 4 5 6 7 8 9 10
vec =
Columns 1 through 7
1.0000 1.2500 1.5000 1.7500 2.0000 2.2500 2.5000
Columns 8 through 14
2.7500 3.0000 3.2500 3.5000 3.7500 4.0000 4.2500
Columns 15 through 21
4.5000 4.7500 5.0000 5.2500 5.5000 5.7500 6.0000
Columns 22 through 28
6.2500 6.5000 6.7500 7.0000 7.2500 7.5000 7.7500
Columns 29 through 35
8.0000 8.2500 8.5000 8.7500 9.0000 9.2500 9.5000
Columns 36 through 37
9.7500 10.0000
ans =
1.7500
vec =
Columns 1 through 7
1.0000 1.2500 1.5000 33.0000 2.0000 2.2500 2.5000
Columns 8 through 14
2.7500 3.0000 3.2500 3.5000 3.7500 4.0000 4.2500
Columns 15 through 21
4.5000 4.7500 5.0000 5.2500 5.5000 5.7500 6.0000
Columns 22 through 28
6.2500 6.5000 6.7500 7.0000 7.2500 7.5000 7.7500
Columns 29 through 35
8.0000 8.2500 8.5000 8.7500 9.0000 9.2500 9.5000
Columns 36 through 37
9.7500 10.0000
CLC Clear command window.
CLC clears the command window and homes the cursor.
See also HOME.
Reference page in Help browser
doc clc
CLEAR Clear variables and functions from memory.
CLEAR removes all variables from the workspace.
CLEAR VARIABLES does the same thing.
CLEAR GLOBAL removes all global variables.
CLEAR FUNCTIONS removes all compiled MATLAB and MEX-functions.
CLEAR ALL removes all variables, globals, functions and MEX links.
CLEAR ALL at the command prompt also clears the base import list.
CLEAR IMPORT clears the base import list. It can only be issued at the
command prompt. It cannot be used in a function.
CLEAR CLASSES is the same as CLEAR ALL except that class definitions
are also cleared. If any objects exist outside the workspace (say in
userdata or persistent in a locked program file) a warning will be
issued and the class definition will not be cleared. CLEAR CLASSES must
be used if the number or names of fields in a class are changed.
CLEAR JAVA is the same as CLEAR ALL except that java classes on the
dynamic java path (defined using JAVACLASSPATH) are also cleared.
CLEAR VAR1 VAR2 ... clears the variables specified. The wildcard
character '*' can be used to clear variables that match a pattern. For
instance, CLEAR X* clears all the variables in the current workspace
that start with X.
CLEAR -REGEXP PAT1 PAT2 can be used to match all patterns using regular
expressions. This option only clears variables. For more information on
using regular expressions, type "doc regexp" at the command prompt.
If X is global, CLEAR X removes X from the current workspace, but
leaves it accessible to any functions declaring it global.
CLEAR GLOBAL X completely removes the global variable X.
CLEAR GLOBAL -REGEXP PAT removes global variables that match regular
expression patterns.
Note that to clear specific global variables, the GLOBAL option must
come first. Otherwise, all global variables will be cleared.
CLEAR FUN clears the function specified. If FUN has been locked by
MLOCK it will remain in memory. Use a partial path (see PARTIALPATH) to
distinguish between different overloaded versions of FUN. For
instance, 'clear inline/display' clears only the INLINE method for
DISPLAY, leaving any other implementations in memory.
CLEAR ALL, CLEAR FUN, or CLEAR FUNCTIONS also have the side effect of
removing debugging breakpoints and reinitializing persistent variables
since the breakpoints for a function and persistent variables are
cleared whenever the program file changes or is cleared.
Use the functional form of CLEAR, such as CLEAR('name'), when the
variable name or function name is stored in a string.
Examples for pattern matching:
clear a* % Clear variables starting with "a"
clear -regexp ^b\d{3}$ % Clear variables starting with "b" and
% followed by 3 digits
clear -regexp \d % Clear variables containing any digits
See also CLEARVARS, WHO, WHOS, MLOCK, MUNLOCK, PERSISTENT, IMPORT.
Reference page in Help browser
doc clear
</pre><h2>Mathematical Operations<a name="2"></a></h2><pre class="codeinput"><span class="comment">% You can use Matlab as a calculator. For example, here is how you</span>
<span class="comment">% multiply, add, divide, and exponentiate:</span>
3 * 4
3 + 4
3 / 4
3 ^ 4
<span class="comment">% You can also use mathematical operations on vectors and matrices.</span>
<span class="comment">% Element-wise operations (multipying each element of a vector or matrix</span>
<span class="comment">% individually) are performed by using the period . before a mathematical</span>
<span class="comment">% operator such as times *:</span>
vector = [2 2]
vector .* 5
matrix = [2 2; 2 2]
matrix .* 3
<span class="comment">% If you want to multiply matrices as in linear algebra (which is not element-wise)</span>
<span class="comment">% you omit the period before the mathematical operator:</span>
matrix * matrix
[3 1; 9 2; 3 7] * [1; 2]
</pre><pre class="codeoutput">
ans =
12
ans =
7
ans =
0.7500
ans =
81
vector =
2 2
ans =
10 10
matrix =
2 2
2 2
ans =
6 6
6 6
ans =
8 8
8 8
ans =
5
13
17
</pre><h2>Logical operations<a name="3"></a></h2><pre class="codeinput"><span class="comment">% Sometimes you want to know whether two variables are equal or if one</span>
<span class="comment">% variable is larger than the other. This is achieved by logical operators.</span>
<span class="comment">% In Matlab, 1 stands for True and 0 stands for False. For example, say you</span>
<span class="comment">% want to know if the firing rate from neuron1 is greater than the firing</span>
<span class="comment">% rate from neuron2:</span>
<span class="comment">% First let's give the neurons' firing rates</span>
neuronFiringRate1 = 4
neuronFiringRate2 = 3
<span class="comment">% Now you can test whether the two variables are equal:</span>
neuronFiringRate1 == neuronFiringRate2
<span class="comment">% or if neuron1 has a greater firing rate than neuron2?</span>
neuronFiringRate1 > neuronFiringRate2
<span class="comment">% These operations also work on vectors and matrices element-wise, so you</span>
<span class="comment">% can determine whether a number is greater than each element in a matrix:</span>
<span class="comment">% Here is our original matrix</span>
matrix
<span class="comment">% And here is the result of comparing the matrix to the number 6:</span>
6 > matrix
<span class="comment">% Logical operators such as AND and OR are also useful. For example, say</span>
<span class="comment">% that you had had a data set that consisted of animals and whether they</span>
<span class="comment">% were fast and big. Suppose you only wanted the animals that were fast</span>
<span class="comment">% and big. Using logical operators, you can select only the fast and big</span>
<span class="comment">% animals:</span>
<span class="comment">% Here we define the properties of one animal</span>
isBig = 1; <span class="comment">% isBig = true</span>
isFast = 0; <span class="comment">% isFast = false</span>
<span class="comment">% Is it both big and fast? The ampersand & indicates AND</span>
isBig & isFast
<span class="comment">% Suppose you only cared if the animal is big or fast, but either one would</span>
<span class="comment">% do. You could then use the OR operator | :</span>
isBig | isFast
<span class="comment">% Finally, suppose you wanted animals that were small and slow. You can</span>
<span class="comment">% use the negation operator, the tilde ~:</span>
~isBig | ~isFast
<span class="comment">% If you want to find out if they are not equal, then use the tilde and</span>
<span class="comment">% equal sign:</span>
isBig ~= isFast
</pre><pre class="codeoutput">
neuronFiringRate1 =
4
neuronFiringRate2 =
3
ans =
0
ans =
1
matrix =
2 2
2 2
ans =
1 1
1 1
ans =
0
ans =
1
ans =
1
ans =
1
</pre><h2>Selective Indexing<a name="4"></a></h2><pre class="codeinput"><span class="comment">% You can combine what you've learned with assigning elements to variables</span>
<span class="comment">% and logical operators. One of the more useful things you can learn is to</span>
<span class="comment">% use logical operators to access and change elements in vectors and</span>
<span class="comment">% matrices. Suppose you have a vector that goes from 1 to 10:</span>
vec = 1:10
<span class="comment">% Remember you could access each individual element of vec by typing:</span>
vec(1)
vec(2)
vec(3)
<span class="comment">% Suppose that you wanted to only access the elements of vec greater than</span>
<span class="comment">% five. From before, you know that</span>
vec > 5
<span class="comment">% is a vector with zeros for each element in vec that is not greater than</span>
<span class="comment">% five and ones for each element that is. Now, if you type:</span>
vec(vec > 5)
<span class="comment">% You get only the elements of vec greater than 5.</span>
<span class="comment">% This can be useful if you want to set all elements of a vector or matrix</span>
<span class="comment">% to a certain value. For instance, if you wanted the values of vec</span>
<span class="comment">% greater than 5 to be zero, then:</span>
vec(vec > 5) = 0
<span class="comment">% Some other examples of this are:</span>
vec(vec == 5)
vec(vec ~= 5)
<span class="comment">% Some useful functions that we will not cover that also use logical</span>
<span class="comment">% operators and are often useful when comparing two vectors or matrices</span>
<span class="comment">% are:</span>
help <span class="string">ismember</span>
help <span class="string">intersect</span>
help <span class="string">find</span>
help <span class="string">setdiff</span>
help <span class="string">unique</span>
</pre><pre class="codeoutput">
vec =
1 2 3 4 5 6 7 8 9 10
ans =
1
ans =
2
ans =
3
ans =
0 0 0 0 0 1 1 1 1 1
ans =
6 7 8 9 10
vec =
1 2 3 4 5 0 0 0 0 0
ans =
5
ans =
1 2 3 4 0 0 0 0 0
ISMEMBER True for set member.
LIA = ISMEMBER(A,B) for arrays A and B returns an array of the same
size as A containing true where the elements of A are in B and false
otherwise.
LIA = ISMEMBER(A,B,'rows') for matrices A and B with the same number
of columns, returns a vector containing true where the rows of A are
also rows of B and false otherwise.
[LIA,LOCB] = ISMEMBER(A,B) also returns an array LOCB containing the
highest absolute index in B for each element in A which is a member of
B and 0 if there is no such index.
[LIA,LOCB] = ISMEMBER(A,B,'rows') also returns a vector LOCB containing
the highest absolute index in B for each row in A which is a member
of B and 0 if there is no such index.
In a future release, the behavior of ISMEMBER will change including:
- occurrence of indices in LOCB will switch from highest to lowest
- tighter restrictions on combinations of classes
In order to see what impact those changes will have on your code, use:
[LIA,LOCB] = ISMEMBER(A,B,'R2012a')
[LIA,LOCB] = ISMEMBER(A,B,'rows','R2012a')
If the changes in behavior adversely affect your code, you may preserve
the current behavior with:
[LIA,LOCB] = ISMEMBER(A,B,'legacy')
[LIA,LOCB] = ISMEMBER(A,B,'rows','legacy')
Examples:
a = [9 9 8 8 7 7 7 6 6 6 5 5 4 4 2 1 1 1]
b = [1 1 1 3 3 3 3 3 4 4 4 4 4 9 9 9]
[lia1,locb1] = ismember(a,b)
% returns
lia1 = [1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1]
locb1 = [16 16 0 0 0 0 0 0 0 0 0 0 13 13 0 3 3 3]
[lia2,locb2] = ismember(a,b,'R2012a')
% returns
lia2 = [1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1]
locb2 = [14 14 0 0 0 0 0 0 0 0 0 0 9 9 0 1 1 1]
[lia,locb] = ismember([1 NaN 2 3],[3 4 NaN 1])
% NaNs compare as not equal, so this returns
lia = [1 0 0 1], locb = [4 0 0 1]
Class support for inputs A and B, where A and B must be of the same
class unless stated otherwise:
- logical, char, all numeric classes (may combine with double arrays)
- cell arrays of strings (may combine with char arrays)
-- 'rows' option is not supported for cell arrays
- objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE
-- including heterogeneous arrays derived from the same root class
See also UNIQUE, UNION, INTERSECT, SETDIFF, SETXOR, SORT, SORTROWS.
Overloaded methods:
cell/ismember
ordinal/ismember
nominal/ismember
categorical/ismember
Reference page in Help browser
doc ismember
INTERSECT Set intersection.
C = INTERSECT(A,B) for vectors A and B, returns the values common to
the two vectors with no repetitions. C will be sorted.
C = INTERSECT(A,B,'rows') for matrices A and B with the same
number of columns, returns the rows common to the two matrices. The
rows of the matrix C will be in sorted order.
[C,IA,IB] = INTERSECT(A,B) also returns index vectors IA and IB such
that C = A(IA) and C = B(IB). If there are repeated common values in
A or B then the index of the last occurrence of each repeated value is
returned.
[C,IA,IB] = INTERSECT(A,B,'rows') also returns index vectors IA and IB
such that C = A(IA,:) and C = B(IB,:).
[C,IA,IB] = INTERSECT(A,B,'stable') for arrays A and B, returns the
values of C in the same order that they appear in A.
[C,IA,IB] = INTERSECT(A,B,'sorted') returns the values of C in sorted
order.
If A and B are row vectors, then C will be a row vector as well,
otherwise C will be a column vector. IA and IB are column vectors.
If there are repeated common values in A or B then the index of the
first occurrence of each repeated value is returned.
[C,IA,IB] = INTERSECT(A,B,'rows','stable') returns the rows of C in the
same order that they appear in A.
[C,IA,IB] = INTERSECT(A,B,'rows','sorted') returns the rows of C in
sorted order.
In a future release, the behavior of the following syntaxes will change
including:
- occurrence of indices in IA and IB will switch from last to first
- orientation of vector C
- IA and IB will always be column index vectors
- tighter restrictions on combinations of classes
In order to see what impact those changes will have on your code, use:
[C,IA,IB] = INTERSECT(A,B,'R2012a')
[C,IA,IB] = INTERSECT(A,B,'rows','R2012a')
If the changes in behavior adversely affect your code, you may preserve
the current behavior with:
[C,IA,IB] = INTERSECT(A,B,'legacy')
[C,IA,IB] = INTERSECT(A,B,'rows','legacy')
Examples:
a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1]
b = [1 1 1 3 3 3 3 3 4 4 4 4 4 10 10 10]
[c1,ia1,ib1] = intersect(a,b)
% returns
c1 = [1 4], ia1 = [21 19], ib1 = [3 13]
[c2,ia2,ib2] = intersect(a,b,'stable')
% returns
c2 = [4 1], ia2 = [19 21]', ib2 = [9 1]'
c = intersect([1 NaN 2 3],[3 4 NaN 1])
% NaNs compare as not equal, so this returns
c = [1 3]
Class support for inputs A and B, where A and B must be of the same
class unless stated otherwise:
- logical, char, all numeric classes (may combine with double arrays)
- cell arrays of strings (may combine with char arrays)
-- 'rows' option is not supported for cell arrays
- objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE
-- including heterogeneous arrays derived from the same root class
See also UNIQUE, UNION, SETDIFF, SETXOR, ISMEMBER, SORT, SORTROWS.
Overloaded methods:
cell/intersect
ordinal/intersect
nominal/intersect
categorical/intersect
Reference page in Help browser
doc intersect
FIND Find indices of nonzero elements.
I = FIND(X) returns the linear indices corresponding to
the nonzero entries of the array X. X may be a logical expression.
Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from
the linear indices I.
I = FIND(X,K) returns at most the first K indices corresponding to
the nonzero entries of the array X. K must be a positive integer,
but can be of any numeric type.
I = FIND(X,K,'first') is the same as I = FIND(X,K).
I = FIND(X,K,'last') returns at most the last K indices corresponding
to the nonzero entries of the array X.
[I,J] = FIND(X,...) returns the row and column indices instead of
linear indices into X. This syntax is especially useful when working
with sparse matrices. If X is an N-dimensional array where N > 2, then
J is a linear index over the N-1 trailing dimensions of X.
[I,J,V] = FIND(X,...) also returns a vector V containing the values
that correspond to the row and column indices I and J.
Example:
A = magic(3)
find(A > 5)
finds the linear indices of the 4 entries of the matrix A that are
greater than 5.
[rows,cols,vals] = find(speye(5))
finds the row and column indices and nonzero values of the 5-by-5
sparse identity matrix.
See also SPARSE, IND2SUB, RELOP, NONZEROS.
Overloaded methods:
codistributed/find
Reference page in Help browser
doc find
SETDIFF Set difference.
C = SETDIFF(A,B) for vectors A and B, returns the values in A that
are not in B with no repetitions. C will be sorted.
C = SETDIFF(A,B,'rows') for matrices A and B with the same number of
columns, returns the rows from A that are not in B. The rows of the
matrix C will be in sorted order.
[C,IA] = SETDIFF(A,B) also returns an index vector IA such that
C = A(IA). If there are repeated values in A that are not in B, then
the index of the last occurrence of each repeated value is returned.
[C,IA] = SETDIFF(A,B,'rows') also returns an index vector IA such that
C = A(IA,:).
[C,IA] = SETDIFF(A,B,'stable') for arrays A and B, returns the values
of C in the order that they appear in A.
[C,IA] = SETDIFF(A,B,'sorted') returns the values of C in sorted order.
If A is a row vector, then C will be a row vector as well, otherwise C
will be a column vector. IA is a column vector. If there are repeated
values in A that are not in B, then the index of the first occurrence of
each repeated value is returned.
[C,IA] = SETDIFF(A,B,'rows','stable') returns the rows of C in the
same order that they appear in A.
[C,IA] = SETDIFF(A,B,'rows','sorted') returns the rows of C in sorted
order.
In a future release, the behavior of the following syntaxes will change
including:
- occurrence of indices in IA will switch from last to first
- orientation of vector C
- IA will always be a column index vector
- tighter restrictions on combinations of classes
In order to see what impact those changes will have on your code, use:
[C,IA] = SETDIFF(A,B,'R2012a')
[C,IA] = SETDIFF(A,B,'rows','R2012a')
If the changes in behavior adversely affect your code, you may preserve
the current behavior with:
[C,IA] = SETDIFF(A,B,'legacy')
[C,IA] = SETDIFF(A,B,'rows','legacy')
Examples:
a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1]
b = [1 1 1 3 3 3 3 3 4 4 4 4 4 10 10 10]
[c1,ia1] = setdiff(a,b)
% returns
c1 = [2 5 6 7 8 9]
ia1 = [20 18 16 13 10 6]
[c2,ia2] = setdiff(a,b,'stable')
% returns
c2 = [9 8 7 6 5 2]
ia2 = [1 7 11 14 17 20]'
c = setdiff([1 NaN 2 3],[3 4 NaN 1])
% NaNs compare as not equal, so this returns
c = [2 NaN]
Class support for inputs A and B, where A and B must be of the same
class unless stated otherwise:
- logical, char, all numeric classes (may combine with double arrays)
- cell arrays of strings (may combine with char arrays)
-- 'rows' option is not supported for cell arrays
- objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE
-- including heterogeneous arrays derived from the same root class
See also UNIQUE, UNION, INTERSECT, SETXOR, ISMEMBER, SORT, SORTROWS.
Overloaded methods:
cell/setdiff
ordinal/setdiff
nominal/setdiff
categorical/setdiff
Reference page in Help browser
doc setdiff
UNIQUE Set unique.
C = UNIQUE(A) for the array A returns the same values as in A but with
no repetitions. C will be sorted.
C = UNIQUE(A,'rows') for the matrix A returns the unique rows of A.
The rows of the matrix C will be in sorted order.
[C,IA,IC] = UNIQUE(A) also returns index vectors IA and IC such that
C = A(IA) and A = C(IC).
[C,IA,IC] = UNIQUE(A,'rows') also returns index vectors IA and IC such
that C = A(IA,:) and A = C(IC,:).
[C,IA,IC] = UNIQUE(A,OCCURRENCE) and
[C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE) specify which index is returned
in IA in the case of repeated values (or rows) in A. The default value
is OCCURENCE='last', which returns the index of the last occurrence of
each repeated value (or row) in A, while OCCURRENCE='first' returns the
index of the first occurrence of each repeated value (or row) in A.
[C,IA,IC] = UNIQUE(A,'stable') returns the values of C in the same order
that they appear in A, while [C,IA,IC] = UNIQUE(A,'sorted') returns the
values of C in sorted order. If A is a row vector, then C will be a row
vector as well, otherwise C will be a column vector. IA and IC are
column vectors. If there are repeated values in A, then IA returns the
index of the first occurrence of each repeated value.
[C,IA,IC] = UNIQUE(A,'rows','stable') returns the rows of C in the same
order that they appear in A, while [C,IA,IC] = UNIQUE(A,'rows','sorted')
returns the rows of C in sorted order.
In a future release, the behavior of the following syntaxes will change
including:
- Default occurrence of indices will switch from last to first
- IA and IC will always be column index vectors
In order to see what impact those changes will have on your code, use:
[C,IA,IC] = UNIQUE(A,'R2012a')
[C,IA,IC] = UNIQUE(A,'rows','R2012a')
[C,IA,IC] = UNIQUE(A,OCCURRENCE,'R2012a')
[C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE,'R2012a')
If the changes in behavior adversely affect your code, you may preserve
the current behavior with:
[C,IA,IC] = UNIQUE(A,'legacy')
[C,IA,IC] = UNIQUE(A,'rows','legacy')
[C,IA,IC] = UNIQUE(A,OCCURRENCE,'legacy')
[C,IA,IC] = UNIQUE(A,'rows',OCCURRENCE,'legacy')
Examples:
a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1]
[c1,ia1,ic1] = unique(a)
% returns
c1 = [1 2 4 5 6 7 8 9]
ia1 = [21 20 19 18 16 13 10 6]
ic1 = [8 8 8 8 8 8 7 7 7 7 6 6 6 5 5 5 4 4 3 2 1]
[c2,ia2,ic2] = unique(a,'stable')
% returns
c2 = [9 8 7 6 5 4 2 1]
ia2 = [1 7 11 14 17 19 20 21]'
ic2 = [1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 6 7 8]'
c = unique([1 NaN NaN 2])
% NaNs compare as not equal, so this returns
c = [1 2 NaN NaN]
Class support for input A:
- logical, char, all numeric classes
- cell arrays of strings
-- 'rows' option is not supported for cell arrays
- objects with methods SORT (SORTROWS for the 'rows' option) and NE
-- including heterogeneous arrays
See also UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER, SORT, SORTROWS.
Overloaded methods:
cell/unique
RTW.unique
dataset/unique
categorical/unique
Reference page in Help browser
doc unique
</pre><h2>Control Loops - For and If<a name="5"></a></h2><pre class="codeinput"><span class="comment">% For loops are useful if you need to repeat a section of code several</span>
<span class="comment">% times. A simple example would be:</span>
<span class="keyword">for</span> ind = 1:20
ex(ind) = ind
<span class="keyword">end</span>
<span class="comment">% Notice that this is the same as</span>
ex = 1:20;
<span class="comment">% Often in Matlab, for loops can be avoided and their vector</span>
<span class="comment">% representations (like above) will be faster and easier to use. When</span>
<span class="comment">% possible, you should always try to use the vector representation.</span>
<span class="comment">% However, you shouldn't waste a whole lot of time trying to figure out the</span>
<span class="comment">% vector representation if a quick and easy for loop will do.</span>
<span class="comment">% If statements are also useful in Matlab. There are occasions when you</span>
<span class="comment">% will want to execute a section of code only if a certain condition is</span>
<span class="comment">% fulfilled.</span>
<span class="comment">% isBig is true</span>