« Earlier8 items total Later »

Don't get annoyed by gradient, et al.

In the interest of "convenience", many MATLAB functions swap the first two dimensions of a multi-dimensional array to preserve some kind of consistency with relating x-y cartesian plots and row-column matrices. At the terrible cost of great confusion in cases like the following:
%% The gradient function
%
% Consider:
%
% $$ w(x,y,z) = x + 2y + 3z $$
%
% $$ \nabla w = (1,2,3) \quad\forall x,y,z$$
%
% Let's try to do this in MATLAB:
w = zeros(5,5,5);
for x = 1:5
  for y = 1:5
    for z = 1:5
      w(x,y,z) = x + 2*y + 3*z;
    end
  end
end

[gx gy gz] = gradient(w);
grad = [gx(1) gy(1) gz(1)]

Instead, to get predictable results, do it like this:
xx = 1:5;
yy = 1:5;
zz = 1:5;
[x,y,z] = meshgrid(xx,yy,zz);

w = x + 2*y + 3*z;

[gx gy gz] = gradient(w);
grad = [gx(1) gy(1) gz(1)]

Extract last k rows from a matrix

>> k = 2;
>> a = rand(5)

a =

    0.1934    0.6979    0.4966    0.6602    0.7271
    0.6822    0.3784    0.8998    0.3420    0.3093
    0.3028    0.8600    0.8216    0.2897    0.8385
    0.5417    0.8537    0.6449    0.3412    0.5681
    0.1509    0.5936    0.8180    0.5341    0.3704

>> a(end-k+1:end,:)

ans =

    0.5417    0.8537    0.6449    0.3412    0.5681
    0.1509    0.5936    0.8180    0.5341    0.3704

Find the distance between each pair of points in a matrix

points = [ 3 4 5;
              6 5 7;
              6 7 1;
              7 6 0;
              3 2 4;
              1 0 3;
              5 6 2;
              9 0 1]

   m=8;
   p=3;


By Peter Aclam
D= sqrt(sum(abs(repmat(permute(points, [1 3 2]), [1 m 1]) ...
   - repmat(permute(points, [3 1 2]), [m 1 1]) ).^2,3));


another solution by Peter Acklam which exploits symmetry
[ i j ] = find(triu(ones(m),1));
E= zeros(m,m);
E(i+m*(j-1) ) = sqrt(sum(abs(points(i,:) -points(j,:)).^2,2));
E(j + m*(i-1)) = E(i+m*(j-1));


And here's a solution by Jyri Kivinen based on the notes in
Borg and Groenen's "Modern Multidimensional Scaling: Theory and
Applications"
K=points*points';
d=diag(K);
one=ones(length(d),1);
D=sqrt(d*one'+one*d'-2*K);


Multiply every column of matrix X by another vector V

Data
X = rand(1000,500);
V = rand(1,1000);

Non vectorized
X2 = zeros(1000,500)
for k = 1:500
   X2(:,k) = V.*X(:,k);
end


Fastest method

X2 = zeros(1000,500)
X2 = diag(sparse(V))*X;

Replace each element with a 4x4 matrix of that element

x=[1 2 ; 4 5 ];
y=kron(x,ones(4))


or

A = [1 2 ; 4 5 ];
A = A(ceil((1:(size(A,1)*4))/4),ceil((1:(size(A,2)*4))/4))


both give

A =

     1     1     1     1     2     2     2     2
     1     1     1     1     2     2     2     2
     1     1     1     1     2     2     2     2
     1     1     1     1     2     2     2     2
     4     4     4     4     5     5     5     5
     4     4     4     4     5     5     5     5
     4     4     4     4     5     5     5     5
     4     4     4     4     5     5     5     5

Swap rows of a matrix

M([1,5],:) = M([5,1],:)

interchanges rows 1 and 5 of M.

More generally, if V has m components and W has n components, then M(V,W) is the m-by-n matrix formed from the elements of M whose subscripts are the elements of V and W.

Reverse one column or row of a matrix

To reverse column or row c of a Matrix M
M(:,c)=flipud(M(:,c))


M(c,:)=flipud(M(c,:))

Create a 1000 by 1000 vector fulls of 7s

X=7*ones(1000,1000);

X=repmat(7,1000,1000);

X(1:1000,1:1000)=7;

« Earlier8 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.