clear
% Test array indexing at right side of assignment.
count5x5 = [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]
magic_7=[30,39,48,1,10,19,28;38,47,7,9,18,27,29;46,6,8,17,26,35,37;5,14,16,25,34,36,45;13,15,24,33,42,44,4;21,23,32,41,43,3,12;22,31,40,49,2,11,20] % magic(7)
A = [1,2,3; 4,5,6; 7,8,9]
A(1)
A(2)
A(3)
A(4)
A(5)
A(10) % error
A(2,3)
A(4,3) % error
A = [1,2,3,4,5,6,7,8,9]
A(1)
A(2)
A(3)
A(4)
A(5)
A(10) % error
A(2,3) % error
A(4,3) % error
A = [1,2,3,4,5;6,7,8,9,10]
A(1)
A(2)
A(3)
A(4)
A(5)
A(10)
A(2,3)
A(4,3) % error
A=magic_7
% by linear index
A(1) % 30
A(3) % 46
A(7) % 22
A(8) % 39
A(15) % 48
A(7*7) % 20
% by subscripts
A(1,1)
A(1,2)
A(2,1)
A
A([1,2,3;4,5,6])
A
A([1,2;3,4],[1,2,3])
A
A(50) % error
A(9,3) % error
A(3,10) % error
A([1,2,3;4,5,9],[1,2,6]) % error
A([1,2,3;4,5,6],[1,2,10]) % error
% Testing dimensions
B=[A,A(1:7,1)]
size(B)
size(B,1)
size(B,2)
size(B,1,1)
size(B,[1,2])
size(1)
C=[]
size(C)
A*C % error
C*A % error
1*C
C*1
sub2ind ([3,3],[2,2,1,1],[1,3,3,2]) % [2,8,7,4]
sub2ind ([3,3],[2,2;1,1],[1,3;3,2]) % [2,8;7,4]
B=[1,2,3;1:3;4,5,6]
B=A^2
C=[A,A(1:7,1)]
D=[]
E=1:7
F=[1:7;2:8]
G=[1:7]'
H=[1:7;2:8]'
[1,2;3,4](1,2) % literal indexing
% test special indexing ('end' and collon (:))
clear
A = [1,2,3,4,5; 6,7,8,9,10; 11,12,13,14,15]
A(2,5)
A(2,end)
A(3,4)
A(3,end-1)
B = [16,5,9,4,2,11,7,14]
B([5:8, 1:4]) % Extract and swap the halves of B
B(1:2:end)   % Extract all the odd elements
B(end:-1:1)   % Reverse the order of elements
A = [1,2,8; 4,4,6; 1,8,9]
A(2,1:end)
A(2,:)
A(1:end) = [2,3,4,5,6,7,8,9,0]
A(2,:) = [1,2,3]
% test logical indexing
clear
A=[30,39,48,1,10,19,28;38,47,7,9,18,27,29;46,6,8,17,26,35,37;5,14,16,25,34,36,45;13,15,24,33,42,44,4;21,23,32,41,43,3,12;22,31,40,49,2,11,20]
A(A(22)) % 30
B=A>29
A(A>29)
B
A(B)
B
A(B)=[1,2,3] % error
B
A(B)=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
B
A(B)=[99,99,99,99,99;99,99,99,99,99;99,99,99,99,99;99,99,99,99,99]
C=[17,24,1,8,15;23,5,7,14,16;4,6,13,20,22;10,12,19,21,3;11,18,25,2,9]
C(B) % error
A(C>20)
% logical indexing with operation
A=[30,39,48,1,10,19,28;38,47,7,9,18,27,29;46,6,8,17,26,35,37;5,14,16,25,34,36,45;13,15,24,33,42,44,4;21,23,32,41,43,3,12;22,31,40,49,2,11,20]
A(B)+=[99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99] % error
A(B)+=[99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99].'
% literal logical indexing
[1,2;3,4]([false,false;true,true])

