Contents
Scalars:
x=2
x*3
x =
2
ans =
6
Vectors/matrices:
x=[1 2 3; 4 5 6; 7 8 9]
x(1,1)
x(1,1:2)
x(1,:)
x(1,3)= x(end, end)
x*x
x.*x
x =
1 2 3
4 5 6
7 8 9
ans =
1
ans =
1 2
ans =
1 2 3
x =
1 2 9
4 5 6
7 8 9
ans =
72 84 102
66 81 120
102 126 192
ans =
1 4 81
16 25 36
49 64 81
Conditional Indexing: Your new best friend
clear all
time=[1:10000];
sample1=abs(100*cos(time/400));
sample2=100*sin(time/10000);
figure(1)
plot(time,sample1,'.',time,sample2,'.')
legend({'sample 1' 'sample 2'})
Conditional indexing part 2
figure(2)
plot(sample1>sample2,'linewidth',2)
tic
x=time(sample1>sample2);
y=sample1(sample1>sample2);
y2=sample2(sample1>sample2);
t1=toc;
tic
j=1;
for i=1:10000
if sample1(i)>sample2(i)
xif(j)=time(i);
yif(j)=sample1(i);
yif2(j)=sample2(i);
j=j+1;
end
end
t2=toc;
conditional indexing part 3
figure(3)
plot(x,y,'.',x,y2,'.')
ylim([0 100])
xlim([0 10000])
sprintf('Conditional indexing method took %0.2g seconds',t1)
sprintf('For loop and if statement method took %0.2g seconds',t2)
sprintf('Conditional indexing is %0.2g times faster',t2/t1)
ans =
Conditional indexing method took 0.00075 seconds
ans =
For loop and if statement method took 0.03 seconds
ans =
Conditional indexing is 40 times faster
Structures and Properties
clear all
sample.material='Zircaloy-2';
sample.type='tensile';
sample.data=0:0.2:5;
sample.rate=1e-4;
Structures and Properties 2
sample(2).material='Excel';
sample(2).type='compression';
sample(2).data=0:-0.2:-5;
sample(2).rate=1e-5;
Samples and Properties 3
x=sample([sample.rate]==1e-5);