%% MATLAB Recipes for Earth Sciences - Chapter 2

%% Section 2.3
clear

%%
A = [2 4 3 7; 9 3 -1 2; 1 9 3 7; 6 6 3 -2]

%%
A = [2 4 3 7; 9 3 -1 2; 1 9 3 7; 6 6 3 -2];

%%
sum(A)

%%
b = sum(sum(A));

%%
whos

%%
clear ans

%%
A(3,2)

%%
A(3,2) = 30

%%
A(3,1:4) = [1 3 3 5]

%%
A(3,:) = [1 3 3 5]

%%
c = 0 : 10

%%
c = 1 : 0.5 : 10

%%
B = [4 2 6 5; 7 8 5 6; 2 1 -8 -9; 3 1 2 3];

%%
C = A * B'

%%
C = A .* B

%% Section 2.4
clear

%%
load geochem.txt

%%
whos

%%
save geochem_new.mat

%%
save geochem_new.mat geochem

%%
save geochem_new.txt geochem -ascii

%% Section 2.5
clear

%%
rand('seed',0)
A = rand(3,4)

%%
format long

%%
A

%%
B = single(A)

%%
format short
A(1,3) = 4i + 3

%%
clear
I1 = 256 * rand(100,100); I1 = ceil(I1);
I2 = 256 * rand(100,100); I2 = ceil(I2);
I3 = 256 * rand(100,100); I3 = ceil(I3);

%%
I = cat(3,I1,I2,I3);

%%
I = uint8(I);

%%
imshow(I)

%%
sample_1.location = 'Plougasnou';
sample_1.date = date;
sample_1.image = I;
sample_1.geochemistry = rand(10,10);

%%
location = 'Plougasnou';

%%
whos location

%%
sample_1.date

%%
sample_1

%%
whos sample_1

%% Section 2.6
clear

%%
x = [3 6 2 -3 8];

%%
average

%%
whos

%%
a = average

%%
help average

%%
clear

%%
data = [3 6 2 -3 8];

%%
result = average(data);

%%
whos

%% Section 2.7
clear

%%
x = 0 : pi/10 : 2*pi;
y = sin(x);

%%
plot(x,y)

%%
x = 0 : pi/10 : 2*pi;
y1 = sin(x);
y2 = cos(x);

%%
plot(x,y1,'r--',x,y2,'b-')

%%
plot(x,y1,'r--')
figure
plot(x,y2,'b-')

%%
plot(x,y1,'r--')
hold on
bar(x,y2)
hold off

%%
subplot(2,1,1), plot(x,y1,'r--')
subplot(2,1,2), bar(x,y2)

%%
clf

%%
plot(x,y1,'r--')
axis([0 pi -1 1])

%%
plot(x,y1,'r--')
axis square

%%
plot(x,y1,'r--')
axis equal

%%
plot(x,y1,'r--')
title('My first plot')
xlabel('x-axis')
ylabel('y-axis')
grid

%% Section 2.8
clear

%%
x = 0 : pi/10 : 2*pi;
y1 = sin(x);
plot(x,y1)

%%
mygraph(x,y1)
