Boolean and logical indexing done

This commit is contained in:
2015-11-05 19:57:51 +01:00
parent 91dc9e1f3b
commit 0c8d6625f8
5 changed files with 489 additions and 294 deletions

View 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

View 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'

View 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)

View 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))