% Indexing a = 100:-1:1 found = find(isprime(a)) b = a(found) % found is a list of integers representing indices of a % An example of permutation, but 'permute' or 'randperm' might be faster arun = zeros(1,5); arun([2 1 4 3 5]) = 10:15; % 10 => arun(2), 11 => arun(1), ... % Logical indexing; return values of 'a' for which 'logicals' is true. % Don't confuse this with indexing an array with another array! Logical % indexing must be done with an array of data-type 'logical' and must have the % same length as the array being indexed. logicals = isprime(a) c = a(logicals) c(12) = 33; b(b == c) intersect(b,c) % Equivalent, but sorted in order % Logical operators evens = (mod(a,2) == 0) a(logicals & evens) a(logicals | evens) % Short-circuit operator example % x = (denom ~= 0) && (numer / denom > 6) % Outer products: in CNS, these are especially useful for creating stimuli % that are identical for most time steps in a simulation row_in = [ones(1,20) 5*ones(1,20) 3*ones(1,20)]; col_in = [1/60:1/60:1]'; in = col_in * row_in; [y,x] = meshgrid(1:60,1:60); mesh(x,y,in) % Concatenation is just plain neat and a good trick to have handy x = [1 2 3; 4 5 6; 7 8 9; 10 11 12]; y = [1 1 1; 2 2 2; 3 3 3; 4 4 4]; t = [0 0 1; 1 0 0; 0 0 1; 1 1 0]; points = [x(:) y(:) t(:)]; % Ready to be output as a table mat3d = reshape(points, [4 3 3]); % multidimensional array