Boolean and logical indexing done
This commit is contained in:
21
programming/code/logicalIndexingBenchmark.m
Normal file
21
programming/code/logicalIndexingBenchmark.m
Normal file
@@ -0,0 +1,21 @@
|
||||
% create a vector with a random numbers
|
||||
x = rand(1000000, 1);
|
||||
|
||||
fprintf('time needed to manually filter elements smaller than 0.5 in a vector of length %i\n', length(x))
|
||||
|
||||
tic
|
||||
results = [];
|
||||
matches = 1;
|
||||
for i = 1:length(x)
|
||||
if x(i) < 0.5
|
||||
results(matches) = x(i);
|
||||
matches = matches + 1;
|
||||
end
|
||||
end
|
||||
toc
|
||||
|
||||
fprintf('\ntime needed to do the same with logical indexing\n')
|
||||
|
||||
tic
|
||||
results = x(x < 0.5);
|
||||
toc
|
||||
12
programming/code/logicalIndexingTime.m
Normal file
12
programming/code/logicalIndexingTime.m
Normal file
@@ -0,0 +1,12 @@
|
||||
t = 0:0.001:10;
|
||||
x = randn(size(t));
|
||||
selection = x (t > 5 & t < 6);
|
||||
|
||||
figure()
|
||||
hold on
|
||||
plot(t, x, 'displayname', 'measurements')
|
||||
plot(t(t > 5 & t < 6), selection, 'displayname', 'selection')
|
||||
xlabel('time [s]')
|
||||
ylabel('intensity')
|
||||
legend 'show'
|
||||
box 'off'
|
||||
12
programming/code/logicalVector.m
Normal file
12
programming/code/logicalVector.m
Normal file
@@ -0,0 +1,12 @@
|
||||
x = 1:10;
|
||||
|
||||
%% Erstellen eines logischen Vektors
|
||||
y = x < 5;
|
||||
fprintf('Logischer Vektor y:\n');
|
||||
y
|
||||
fprintf('Datentyp von y: %s\n', class(y));
|
||||
|
||||
%% Auswahl aller Element, die kleiner 5 sind
|
||||
fprintf('\nAlle Elemente aus x, die kleiner als 5 sind:\n')
|
||||
|
||||
x(y)
|
||||
9
programming/code/vectorsize.m
Normal file
9
programming/code/vectorsize.m
Normal file
@@ -0,0 +1,9 @@
|
||||
a = (11:20); % a vector with 10 Elements
|
||||
fprintf('length of a: %i\n', length(a))
|
||||
size_of_a = size(a); % get the size and store it in a new variable
|
||||
|
||||
% size_of_a is a vector itself
|
||||
fprintf('number of dimensions (rank) of size_of_a: %i\n', length(size_of_a))
|
||||
|
||||
% get the value of the second element of size_of_a
|
||||
fprintf('number of entries in the 2nd dimesion of a: %i\n', size_of_a(2))
|
||||
Reference in New Issue
Block a user