Showing posts with label MatLab. Show all posts
Showing posts with label MatLab. Show all posts

Oct 3, 2013

Matlab useful tricks

  • Finding a number to particular value:
    • example: Here if we want to find ratio close to 0.7 then we will look for ar for   the current image to be close to 0.7.
                aspect_ratio = [0.5 0.7 1.0 1.25 1.5 1.75 2.0 2.25 2.5 3];           
                ar = imgBB.width/imgBB.height
                tmp = abs(aspect_ratio - ar); 
                [val, idx] = min(tmp);  

                if (idx == 2) then print .... found it.
  •  

Jul 19, 2013

Standardize the data

%% standardize the data
numDsc = size(X,1);
fprintf('numDsc = %d\n',numDsc);
meanData = mean(X); % column-wise mean
meanDuplicate = meanData(ones(1,numDsc), :); % replicate
normData = X - meanDuplicate;
stdData = std(X); % column-wise std
stdData(stdData == 0) = 1; % if standard deviation is 0, set to 1 to avoid zero-denominator problems in normalization
stdDuplicate = stdData(ones(1,numDsc), :); % replicate
normData = normData ./stdDuplicate;

Jul 9, 2013

Confusin matrix

% compute and display test error
num_classes = 4;
y = [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4]';
pred_l = [1 1 2 3 2 2 3 4 3 3 4 1 4 4 1 2]';
conf_matrix = zeros(num_classes, num_classes);
for c = 1:num_classes  
    indices = find(y==c);
    for c2 = 1:num_classes
        conf_matrix(c, c2) = sum(pred_l(indices)==c2);
    end
    disp([c ' error: ' num2str(sum(pred_l(indices)~=c))]);
end
disp('Confusion Matrix: ');
disp(num2str(conf_matrix));

Dec 7, 2012

Matlab installation from iso

reference
  • mounts on:
    • sudo mount -o loop PATHOFTHEDOTISOFILE /media/iso
  • license is in the crack/install.txt file
  • activation should be shown to 
    • crack/lic_standalone.dat
    • for some version (bin/glnx_64 folder should be copied into /usr/local/MATLAB/Rxxxxx/bin/)

Nov 14, 2012

Saving the figure from the handle

saveas: saveas(f, ['output/plane_color/' num2str(fnum) '_plane_in_image.fig']);
hgsave: hgsave(f, ['output/plane_color/' num2str(fnum) '_plane_in_image.fig']);

Nov 11, 2012

Jun 7, 2012

Matlab: showing axis in plot3

>> plot(x,y) % create the plot
>> xlabel('x (radians)'); % label the x-axis
>> ylabel('sine function'); % label the y-axis
>> title('sin(3*x)');
linePlots

May 29, 2012

Matlab Utility reading a file:

fid1 = fopen('trajectory_from_plane_comp.txt', 'r');
while ~feof(fid1)
    temp = textscan(fid1, '%s %d %f %f %f %f', 1, 'delimiter', ' ');
    data_1 = [data_1; temp{3} temp{4} temp{5} temp{6}];
end
fclose(fid1);

Mar 22, 2012

NaN and Inf experiment Matlab

>> a = [1 nan nan]
a = 1 NaN NaN

>> isnan(a)
ans = 0 1 1

>> b = [inf inf 0]
b = Inf Inf 0

>> isinf(b)
ans = 1 1 0

>> c = [NaN inf NaN 3]
c = NaN Inf NaN 3

>> isnan(c)
ans = 1 0 1 0

>> isinf(c)
ans = 0 1 0 0

>> c = c(1:3)
c = NaN Inf NaN

>> d = [a; 1 2 3; b; 4 5 6; c; c; a; 1 1 1]
d = 1 NaN NaN
1 2 3
Inf Inf 0
4 5 6
NaN Inf NaN
NaN Inf NaN
1 NaN NaN
1 1 1

 >> x = d(:,1)
x = 1 1 Inf 4 NaN NaN 1 1

>> y = d(:,2)
y = NaN 2 Inf 5 Inf Inf NaN 1

>> z = d(:,3) z = NaN 3 0 6 NaN NaN NaN 1

>> valid = ~isnan(x)
valid = 1 1 1 1 0 0 1 1

>> valid = ~isnan(x) & ~isinf(x)
valid = 1 1 0 1 0 0 1 1

>> valid = ~isnan(y) & ~isinf(y)
valid = 0 1 0 1 0 0 0 1

>> valid = ~isnan(z) & ~isinf(z)
valid = 0 1 1 1 0 0 0 1

Mar 21, 2012

Matlab symbolplots

symbolPlots

Random number in negative and positive range in Matlab

http://www.dsprelated.com/groups/matlab/show/6209.php

Superimposing points, lines over image in Matlab

figure
imshow(img)
plot(x_c, y_c, '.');
line_x = [x_c - round(w_c/2), x_c + round(w_c/2), x_c + round(w_c/2), x_c - round(w_c/2), x_c - round(w_c/2)];
line_y = [y_c - round(h_c/2), y_c - round(h_c/2), y_c + round(h_c/2), y_c + round(h_c/2), y_c - round(h_c/2)];
line(line_x, line_y, 'LineWidth',1,'Color',[.8 .8 .8]);

Other reference

Feb 3, 2012

Matlab parsing a string by a delimiter

Reference: regexp
inputTxt=textscan(fid,'%s',1,'delimiter','\n');
input=inputTxt{1};
str = cellstr(input)
disp(input);
sElem = regexp(str, ' ', 'split');
splitedElem = sElem{1};

Jan 24, 2012

Matlab updating the path directory

Append new directory:
p = path;
path(p, '/Users/hello/Desktop/directory/);

Remove a directory from the path:

rmpath('/Users/hello/Desktop/directory/);


Reference

Jan 18, 2012

Fitting gaussian mixture model to the data

Here I have generated a mixture distribution using two gaussians: 1) Mean=3, Variance=1, 2) Mean=10, Variance=2, respectively. Then fit the mixture distribution using Matlab's gmdistribution.fit(), which estimates the mixture model parameters using Expectation Maximization algorithm.

MU1 = [3];
SIGMA1 = [1];
MU2 = [10];
SIGMA2 = [2];
X = [mvnrnd(MU1,SIGMA1,100);
mvnrnd(MU2,SIGMA2,100)];
% scatter(X(:,1),X(:,2),10,'.')
figure hist(X) title('histogram')
pause;
close;
options = statset('Display','final');
obj = gmdistribution.fit(X,2,'Options',options);
ComponentMeans = obj.mu
ComponentCovariances = obj.Sigma


Reference from Mathworks Matlab

Plotting multiple histograms in different color: Matlab

histograms-of-two-set-of-data-with-different-color-in-matlab

Game engines

Unity engine   Unreal engine Godot engine