Turning y axis upside down in MATLAB

The ‘YDir’ axes property can be either ‘normal’ or ‘reverse’. By default it is ‘normal’ for most plots, but some plots will automatically change it to ‘reverse’, such as the image or imagesc functions. You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions): … Read more

How to initialize an array of structs in MATLAB?

Using repmat is by far the most efficient way to preallocate structs : N = 10000; b = repmat(struct(‘x’,1), N, 1 ); This is ~10x faster using Matlab 2011a than preallocating via indexing, as in N = 10000; b(N).x = 1 The indexing method is only marginally faster than not preallocating. No preallocation: 0.075524 Preallocate … Read more

How to use SIFT algorithm to compute how similar two images are?

First, aren’t you supposed to be using vl_sift instead of sift? Second, you can use SIFT feature matching to find correspondences in the two images. Here’s some sample code: I = imread(‘p1.jpg’); J = imread(‘p2.jpg’); I = single(rgb2gray(I)); % Conversion to single is recommended J = single(rgb2gray(J)); % in the documentation [F1 D1] = vl_sift(I); … Read more

how to count unique elements of a cell in matlab?

To count unique elements, you can combine UNIQUE with ACCUMARRAY c = {‘a’, ‘b’, ‘c’, ‘a’}; [uniqueC,~,idx] = unique(c); %# uniqueC are unique entries in c %# replace the tilde with ‘dummy’ if pre-R2008a counts = accumarray(idx(:),1,[],@sum); To produce the structure, use NUM2CELL and STRUCT: countCell = num2cell(counts); tmp = [uniqueC;countCell’]; %’ unique_count = struct(tmp{:}) … Read more