PHYS108, HW2
Aparna Sarkar
10/2/2013
HW2


1.
a=[-1 0 3];
b = [0 3 1];
~a
ans =
     0     1     0
a&b
ans =
     0     0     1
a|b
ans =
     1     1     1
a>0 & b>0
ans =
     0     0     1
a>0 | b>0
ans =
     0     1     1
~a>0
ans =
     0     1     0
~a>b
ans =
     0     0     0
~(a>b)
ans =
     1     1     0

2.
v= [1 2 5 0 5]
v =
     1     2     5     0     5
x = v;
x(find(x==max(v))) = []
x =
     1     2     0

3.
a= [5 4];
b=[3 2];
c= [ 1 0 ];
(a>b)&(b>c)
ans =
     1     1
x = [.5 .5];
y = [.3 .3];
abs(x-y)>0 & abs(x+y)<1
ans =
     1     1
c = [ 1 0 ];
d = [7 8];
( c<d | a<b ) & ~(c<d & a<b)
ans =
     1     1
x = y
x =
    0.3000    0.3000
z= [0 1];
(x == y) & (y~=z)
ans =
     1     1

4.
A= [1 0 1 0 ; 3 5 1 2 ; 7 5 6 3; 0 1 1 0];
B = all(A== A')
B =
     0     0     0     0
A = [1 0 1 0; 0 2 3 1 ; 1 3 0 2; 0 1 2 3]
A =
     1     0     1     0
     0     2     3     1
     1     3     0     2
     0     1     2     3
B = all(A==A')
B =
     1     1     1     1

v = [1 1.4 -.7 4]
v =
    1.0000    1.4000   -0.7000    4.0000
range = find((v<=1.5 & v>=-1) & (sin(v)-cos(v)>.5))
range =
     2
A = [-3 2 -1; 0 1 0; -5 6 7];
B = A;
B(find(A(:)<0))= 1
B =
     1     2     1
     0     1     0
     1     6     7

5. 6 ways (most efficient: blkdiag-- others are different combinations of cat, vertcat, horzcat)

A = [2 6 ; 3 9 ];
B = [ 1 2 ; 3 4];
C = [-5 5; 5 3];
G=blkdiag(A,B,C)
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3
clear G
G = [horzcat(A,zeros(2),zeros(2)); horzcat(zeros(2),B,zeros(2)); horzcat(zeros(2),zeros(2),C)]
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3
clear G
G = vertcat(horzcat(A,zeros(2),zeros(2)), horzcat(zeros(2),B,zeros(2)), horzcat(zeros(2),zeros(2),C))
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3
G = cat(1,horzcat(A,zeros(2),zeros(2)), horzcat(zeros(2),B,zeros(2)), horzcat(zeros(2),zeros(2),C))
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3
G = cat(2,vertcat(A,zeros(2),zeros(2)), vertcat(zeros(2),B,zeros(2)), vertcat(zeros(2),zeros(2),C))
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3
G = horzcat(vertcat(A,zeros(2),zeros(2)), vertcat(zeros(2),B,zeros(2)), vertcat(zeros(2),zeros(2),C))
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
     0     0     0     0     5     3

6.
G(6,:)=[]
G =
     2     6     0     0     0     0
     3     9     0     0     0     0
     0     0     1     2     0     0
     0     0     3     4     0     0
     0     0     0     0    -5     5
G(:,6)=[]
G =
     2     6     0     0     0
     3     9     0     0     0
     0     0     1     2     0
     0     0     3     4     0
     0     0     0     0    -5

G_1 = G(1:2,1:2)
G_1 =
     2     6
     3     9

G(5,5)=4
G =
     2     6     0     0     0
     3     9     0     0     0
     0     0     1     2     0
     0     0     3     4     0
     0     0     0     0     4

G(13)
ans =
     1
% Because: MATLAB reads this as the 13th element of G , going down the columns consecutively

G(12,1)
{??? Index exceeds matrix dimensions.
} 
%The above happens because the command attempts to access the 12th row of a 5x5 matrix

7.
A= rand(10,10)
A =
  Columns 1 through 7
    0.1622    0.4505    0.1067    0.4314    0.8530    0.4173    0.7803
    0.7943    0.0838    0.9619    0.9106    0.6221    0.0497    0.3897
    0.3112    0.2290    0.0046    0.1818    0.3510    0.9027    0.2417
    0.5285    0.9133    0.7749    0.2638    0.5132    0.9448    0.4039
    0.1656    0.1524    0.8173    0.1455    0.4018    0.4909    0.0965
    0.6020    0.8258    0.8687    0.1361    0.0760    0.4893    0.1320
    0.2630    0.5383    0.0844    0.8693    0.2399    0.3377    0.9421
    0.6541    0.9961    0.3998    0.5797    0.1233    0.9001    0.9561
    0.6892    0.0782    0.2599    0.5499    0.1839    0.3692    0.5752
    0.7482    0.4427    0.8001    0.1450    0.2400    0.1112    0.0598
  Columns 8 through 10
    0.2348    0.5470    0.9294
    0.3532    0.2963    0.7757
    0.8212    0.7447    0.4868
    0.0154    0.1890    0.4359
    0.0430    0.6868    0.4468
    0.1690    0.1835    0.3063
    0.6491    0.3685    0.5085
    0.7317    0.6256    0.5108
    0.6477    0.7802    0.8176
    0.4509    0.0811    0.7948
A = A.*100;
A = round(A)
ans =
    16    45    11    43    85    42    78    23    55    93
    79     8    96    91    62     5    39    35    30    78
    31    23     0    18    35    90    24    82    74    49
    53    91    77    26    51    94    40     2    19    44
    17    15    82    15    40    49    10     4    69    45
    60    83    87    14     8    49    13    17    18    31
    26    54     8    87    24    34    94    65    37    51
    65   100    40    58    12    90    96    73    63    51
    69     8    26    55    18    37    58    65    78    82
    75    44    80    14    24    11     6    45     8    79
A(find(A<10))=0
A =
    16    45    11    43    85    42    78    23    55    93
    79     0    96    91    62     0    39    35    30    78
    31    23     0    18    35    90    24    82    74    49
    53    91    77    26    51    94    40     0    19    44
    17    15    82    15    40    49     0     0    69    45
    60    83    87    14     0    49    13    17    18    31
    26    54     0    87    24    34    94    65    37    51
    65   100    40    58    12    90    96    73    63    51
    69     0    26    55    18    37    58    65    78    82
    75    44    80    14    24    11     0    45     0    79
A(find(A>90))=inf
A =
    16    45    11    43    85    42    78    23    55   Inf
    79     0   Inf   Inf    62     0    39    35    30    78
    31    23     0    18    35    90    24    82    74    49
    53   Inf    77    26    51   Inf    40     0    19    44
    17    15    82    15    40    49     0     0    69    45
    60    83    87    14     0    49    13    17    18    31
    26    54     0    87    24    34   Inf    65    37    51
    65   Inf    40    58    12    90   Inf    73    63    51
    69     0    26    55    18    37    58    65    78    82
    75    44    80    14    24    11     0    45     0    79
v = A(find(A>=30 & A <=60))
v =
    31
    53
    60
    45
    54
    44
    40
    43
    58
    55
    35
    51
    40
    42
    49
    49
    34
    37
    39
    40
    58
    35
    45
    55
    30
    37
    49
    44
    45
    31
    51
    51

8.
A = [1 0 ; 2 1];
B = [1 2 ; 0 1];
A*B
ans =
     1     2
     2     5
B*A
ans =
     5     2
     2     1

9.
v=[1:100];
Cmat=reshape(v,10,10)
Cmat =
     1    11    21    31    41    51    61    71    81    91
     2    12    22    32    42    52    62    72    82    92
     3    13    23    33    43    53    63    73    83    93
     4    14    24    34    44    54    64    74    84    94
     5    15    25    35    45    55    65    75    85    95
     6    16    26    36    46    56    66    76    86    96
     7    17    27    37    47    57    67    77    87    97
     8    18    28    38    48    58    68    78    88    98
     9    19    29    39    49    59    69    79    89    99
    10    20    30    40    50    60    70    80    90   100
S=sum(Cmat)
S =
    55   155   255   355   455   555   655   755   855   955

E=[13 -1 5; -22 10 -87];
mean(E,2)
ans =
    5.6667
  -33.0000

E(1,:)=ones
E =
     1     1     1
   -22    10   -87

C = Cmat(2:9,2:9)
C =
  Columns 1 through 7
    12    22    32    42    52    62    72
    13    23    33    43    53    63    73
    14    24    34    44    54    64    74
    15    25    35    45    55    65    75
    16    26    36    46    56    66    76
    17    27    37    47    57    67    77
    18    28    38    48    58    68    78
    19    29    39    49    59    69    79
  Column 8
    82
    83
    84
    85
    86
    87
    88
    89

v = [1:20];
v(2:2:end)=-v(2:2:end)
v =
  Columns 1 through 7
     1    -2     3    -4     5    -6     7
  Columns 8 through 14
    -8     9   -10    11   -12    13   -14
  Columns 15 through 20
    15   -16    17   -18    19   -20

v=rand(1,15)
v =
  Columns 1 through 4
    0.6443    0.3786    0.8116    0.5328
  Columns 5 through 8
    0.3507    0.9390    0.8759    0.5502
  Columns 9 through 12
    0.6225    0.5870    0.2077    0.3012
  Columns 13 through 15
    0.4709    0.2305    0.8443
v(find(v<0.5))=0.0
v =
  Columns 1 through 4
    0.6443         0    0.8116    0.5328
  Columns 5 through 8
         0    0.9390    0.8759    0.5502
  Columns 9 through 12
    0.6225    0.5870         0         0
  Columns 13 through 15
         0         0    0.8443

10.
A=[1:3];
B=[2 4 8]';
A*B
ans =
    34
B*A
ans =
     2     4     6
     4     8    12
     8    16    24
A'*B'
ans =
     2     4     8
     4     8    16
     6    12    24

11.
A = randint(5,6,[-100,100]);
sort(A,1)
ans =
   -79   -90   -17   -40   -75   -94
   -61   -46   -16     8   -66   -62
   -32    48    10    33   -65    12
   -26    84    89    40    33    34
    -2    91    97    40   100    77
sort(A,2)
ans =
   -94   -79   -40   -16    33    91
   -65   -26    10    12    40    84
   -90   -75   -61    33    77    89
   -17    -2     8    34    48   100
   -66   -62   -46   -32    40    97



