% W. Menke (2024), Geophysical Data Analysis and Inverse Theory
% with MATLAB(R) or Python, 5th Edition
% 2022-10-01, Imported from 4th Edition by W. Menke
% 2023-04-02, Edit and integration with text
% examples of directory manipulation, support Section I.3
% print working directory
% change directory to one up
% change directory back to LiveScripts
% list files and sub-folders in the current directory
dir
. gda_read_resp.m
.. gda_timedelay.m
OLD gdama_01.mlx
gda_Esurface.m gdama_02.mlx
gda_FTFmul.m gdama_03.mlx
gda_FTFrhs.m gdama_04.mlx
gda_PD.m gdama_05.mlx
gda_SD.m gdama_06.mlx
gda_apply_response.m gdama_07.mlx
gda_arrow3.m gdama_08.mlx
gda_bin2int.m gdama_09.mlx
gda_chebyshevfilt.m gdama_10.mlx
gda_delay.m gdama_11.mlx
gda_dlsmul.m gdama_12.mlx
gda_dlsrhs.m gdama_13.mlx
gda_draw.m gdama_14.mlx
gda_filtermul.m gdama_15.mlx
gda_filterrhs.m gdama_16.mlx
gda_gray1table.mat gdama_17.mlx
gda_gray2int.m gdastereo.m
gda_int2bin.m gray1table.mat
gda_int2gray.m
% example of defining vectors and matrices
% define row-vector, column-vector and a matrix
% initialze vectors to zeroes
% initialze vectors to ones
% initialze vectors to ones
A = [ [1, 4, 7]', [2, 5, 8]', [3, 6, 9]'];
% define A, alternate version
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% initialize matrices with zeros() and ones()
A = [ [1, 4, 7]', [2, 5, 8]', [3, 6, 9]'];
B = [ [0, 3, 7]', [5, 5, 9]', [1, 2, 7]'];
disp(P);
27 42 26
57 99 56
87 156 86
% examples of matrix products
A = [ [1, 4, 7]', [2, 5, 8]', [3, 6, 9]'];
B = [ [0, 3, 7]', [5, 5, 9]', [1, 2, 7]'];
disp(P);
27 42 26
57 99 56
87 156 86
% sub-matrices using colon operator
B = [ 1, 2, 3; 4, 5, 6; 7, 8, 9 ];
% top two elements of vector a
disp('x is top two elements of a');
x is top two elements of a
% second columns of matrix B
disp('y is second column of B');
disp('z is second row of B');
% lower-right 2x2 block of B
disp('T is lower-right 2x2 block of B');
T is lower-right 2x2 block of B
% example of the matrix inverse, determinant, eigenvalues
A = [ [1, 5, 13]', [2, 7, 17]', [3, 11, 19]'];
disp(B);
-2.2500 0.5417 0.0417
2.0000 -0.8333 0.1667
-0.2500 0.3750 -0.1250
disp(C);
1.0000 0 0.0000
0.0000 1.0000 0.0000
-0.0000 0 1.0000
disp(D);
1.0000 0.0000 -0.0000
0 1.0000 0.0000
0 0 1.0000
disp(error);
1.0e-15 *
0.1110
-0.8882
0.4441
B = [ [1, 3, 4]', [2, 3, 2]', [0, 0, 4]'];
disp(D);
1.7500 -1.1250 0.3750
-0.7500 -0.8750 0.6250
-6.0000 2.0000 0.0000
% define symmetric matrix M
A = [ [1, 2, 0]', [2, 2, 0]', [0, 0, 4]'];
% eigenvalues and eigenvectors
disp(V);
-0.7882 0.6154 0
0.6154 0.7882 0
0 0 1.0000
disp(LAMBDA);
-0.5616 0 0
0 3.5616 0
0 0 4.0000
E=M-V*LAMBDA*V'
2.0000 1.0000 3.0000
1.0000 1.0000 3.0000
3.0000 3.0000 -1.0000
disp(E);
2.0000 1.0000 3.0000
1.0000 1.0000 3.0000
3.0000 3.0000 -1.0000
% character string formatting amd lists (cellstrs) of character strings
myfilename=sprintf('myfile_%d.txt',i);
% format a fractioanl number
mysentence = sprintf('position x is %.2f meters',x);
disp(mysentence);
position x is 10.40 meters
fprintf('position x is %.2f meters\n',x);
position x is 10.40 meters
% define list f character strings
mycolorlist = {'red', 'crimson', 'chartruse', 'teal'};
% access one element of the list
myfavoritecolor = char(mycolorlist(4));
% define a matrix A and a vector b
A = [ [1, 2, 3]', [4, 5, 6]', [7, 8, 9]'];
% use a for loop to extract the diagonal elements of
% the matrix M into the column-vector a
% example of two nested for loops
% define a matrices M and N
A = [ [1, 4, 7]', [2, 5, 8]', [3, 6, 9]'];
% use two nested for loop to reverse the order
% of the elements in each row of the matrix M
% example of a for loop containing a consitional statement
a = [ 1, 2, 1, 4, 3, 2, 6, 4, 9, 2, 1, 4 ]';
% use two nested for loop to reverse the order
% of the elements in each row of the matrix M
% display vectors side by side
disp([a, b]);
1 1
2 2
1 1
4 4
3 3
2 2
6 6
4 4
9 6
2 2
1 1
4 4
A = [ [1, 2, 3]', [4, 5, 6]', [7, 8, 9]'];
% another way of reversing order order of rows
a = [ 1, 2, 1, 4, 3, 2, 6, 4, 9, 2, 1, 4 ]';
% display vectors side by side
disp('a b (via logical indexing)');
a b (via logical indexing)
disp([a, b]);
1 1
2 2
1 1
4 4
3 3
2 2
6 6
4 4
9 6
2 2
1 1
4 4
% yet another way to clip to 6
% display vectors side by side
disp([a, b]);
1 1
2 2
1 1
4 4
3 3
2 2
6 6
4 4
9 6
2 2
1 1
4 4
% yet another way to clip to 6
% display vectors side by side
disp('a b (via logical functions)');
a b (via logical functions)
disp([a, b]);
1 1
2 2
1 1
4 4
3 3
2 2
6 6
4 4
9 6
2 2
1 1
4 4
% load and plot 1965-2017 global temperature data
% Hansen, J., Mki. Sato, R. Ruedy, K. Lo, D.W. Lea, and M. Medina-Elizade,
% 2006: Global temperature change. Proc. Natl. Acad. Sci., 103, 14288-14293,
% doi:10.1073/pnas.0606291103.
D=load('../Data/global_temp.txt');
% display first few lines
disp([t(1:5),d(1:5)]);
1.0e+03 *
1.9650 -0.0001
1.9660 -0.0001
1.9670 -0.0000
1.9680 -0.0001
1.9690 0.0001
axis( [1965, 2020, -0.5, 1.0] );
plot(t,d,'r-','LineWidth',3); % plot data as red lines
plot(t,d,'ko','LineWidth',3); % plot data as black circles
ylabel('temperature anomaly, deg C');
title('global temperature data');
fprintf('Caption: Global temperature data for the time period 1965–2016\n');
Caption: Global temperature data for the time period 1965–2016
% write data to a text file
% this example writes global temperature data in deg F
cf = 1.8; % decC to degF conversion factor
D=load('../Data/global_temp.txt');
filename = '../TestFolder/global_temp_in_F.txt';
% concatenate output array
% write file with tab as delimiter
dlmwrite(filename,DF,'delimiter','\t');
axis( [1965, 2020, -1.0, 2.0] );
plot(t,dF,'r-','LineWidth',3);
plot(t,dF,'ko','LineWidth',3);
ylabel('temperature anomaly, deg F');
titlestr=sprintf('temperature data from %d to %d',t(1),t(N));
% set the title to the title string
fprintf('Caption: Global temperature data for the time period 1965–2016\n');
Caption: Global temperature data for the time period 1965–2016
% Hansen, J., Mki. Sato, R. Ruedy, K. Lo, D.W. Lea, and M. Medina-Elizade,
% 2006: Global temperature change. Proc. Natl. Acad. Sci., 103, 14288-14293,
% doi:10.1073/pnas.0606291103.
D=load('../Data/global_temp.txt');
disp(sprintf('Number of data: %d\n', N));
% display first few lines
disp([t(1:5),d(1:5)]);
1.0e+03 *
1.9650 -0.0001
1.9660 -0.0001
1.9670 -0.0000
1.9680 -0.0001
1.9690 0.0001
clf; % clears the figure (just to be sure were starting with a blank figure)
% The following 'set' command specifies the line width of the figure axes.
% The figure does not look good when printed to paper when the line width is too thin
% The following 'set' command specifies the font size of characters om the figure axes.
hold on; % dont let the above properties be changed by the plot() command
axis( [1965, 2020, -0.5, 1.0] );
plot(t,d,'r-','LineWidth',3);
plot(t,d,'ko','LineWidth',3);
ylabel('temperature anomaly, deg C');
titlestr=sprintf('temperature data from %d to %d',t(1),t(N))
titlestr = 'temperature data from 1965 to 2016'
% set the title to the title string
fprintf('Caption: Global temperature data for the time period 1965–2016\n');
Caption: Global temperature data for the time period 1965–2016