masdeseiscaracteres
2/21/2014 - 6:02 PM

Some Matlab fun

Some Matlab fun

%% Positive integer lattice points
% (colored according to the projective line they belong to)

N=12;

%% Option 1
[x y]=ind2sub([N N],1:N^2);
scatter(x,y,[],angle(x+1j*y)/(pi/2),'.');
colormap('colorcube'); %Optional
set(gca,'PlotBoxAspectRatio',[1 1 1],'XLim',[0 N+1],'YLim',[0 N+1],'CLim',[0 1],'XGrid','on','YGrid','on');

%% Option 2
[x y]=ind2sub([N N],1:N^2);
[b, i, j]=unique(y./x);
set(axes,'NextPlot','add');
cellfun(@(x)plot(x,'.','Color',rand(3,1)),cellfun(@(t) x(j==t)+1j*y(j==t),num2cell(1:length(i)),'UniformOutput',false));
set(gca,'PlotBoxAspectRatio',[1 1 1],'XLim',[0 N+1],'YLim',[0 N+1],'CLim',[0 1],'XGrid','on','YGrid','on');

%% Option 3
[b, i, j]=unique(kron(1:N,ones(1,N))./kron(ones(1,N),1:N));
set(axes,'NextPlot','add');
cellfun(@(x)plot(x,'.','Color',rand(3,1)),cellfun(@(t) subsref(kron(ones(1,N),1:N),struct('type','()','subs',{{j==t}}))+1j*subsref(kron(1:N,ones(1,N)),struct('type','()','subs',{{j==t}})),num2cell(1:length(i)),'UniformOutput',false));
set(gca,'PlotBoxAspectRatio',[1 1 1],'XLim',[0 N+1],'YLim',[0 N+1],'CLim',[0 1],'XGrid','on','YGrid','on');
%% FUN 1

        % Verify the following matrix identity
        %   vec(ddiag(A*B*C))=S*kron(C.',A)*vec(B)
        % where S=diag(vec(eye(N,M)))

        ddiag=@(x) tril(triu(x));
        vec=@(x) x(:);
        N=3;
        M=5;
        A=crandn(N);
        B=crandn(N,M);
        C=crandn(M);
        
        vec(ddiag(A*B*C))
        diag(vec(eye(N,M)))*kron(C.',A)*vec(B)

%% MORE FUN TO COME
This series of Gists contains little "useless" Matlab scripts I wrote because of different reasons.

I thought it would be handy to have them here. I hope others find them useful as well.