Search this blog!

Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Monday, March 12, 2012

Matlab: legend in Errorbar

I want to generate an error bar like this. Here, I am comparing three algorithms and I need three legends. Here is how I did it



y = [2.22153808961105 2.49790518623828 2.78257964160148]; % mean or average
e = [0.539408440954881 1.23204940330217 0.813491394996712]; % 1 standard deviation
figure
errorbar(y,e,'r.');
hold on;
h=plot(1,y(1),'ro',2,y(2),'go',3,y(3),'bo');
legend(h,'feature','feature3x3','cluster','Location','Best');

Friday, February 24, 2012

In Matlab : Sorting by indexes of another sorting

Imagine, we have the following two array of numbers
A=[ 2 4 6 7 8 8];
 B=[ 5 8 2 5 0 5];
Problem:
---------
we want to sort A and then arrange B according to the index of A.

Solution:
----------

>> [sortedA,indexA]=sort(A)

sortedA =

     2     4     6     7     8     8

indexA =

     1     2     3     4     5     6



>> B(indexA)

ans =

     5     8     2     5     0     5

Thursday, February 16, 2012

How to append in a excel file using Matlab?

Imagine that I have the following excel file called "algoperformace.xls"


algoName mode 95percentile max mean
Algorithm1 13.15153679 20.03323188 39.52043729 15.65858


Now, I would like to add a new line as the following into this excel file using matlab


Neighbor 2.721678458 5.536962451 26.63633753 3.858783


How to do that?
----------------------------------------------------------------------------------
Answer:
%% Loading the excel into matlab

[~, ~, alldata] = xlsread('algperformace.xls');
% We will add new entry line into 'alldata' variable and then will write the excel file
% The script will compute the value for algoname, mode, color_diff_95_percentile, max, mean
% adding those values into the cell of 'alldata'
alldata(size(alldata,1),:)={algoname, mode, color_diff_95_percentile, max, mean};
%writing the complete new 'alldata' into the same excel file
xlswrite('algperformace.xls', alldata);

Try your own example :)


Wednesday, December 14, 2011

In MATLAB: find two tailed P value of t-distribution (Student's t-distribution)

Problem:
-----------
Given the t-ratio and the degrees of freedom(dof), find two tailed P value of t-distribution (Student's t-distribution)

Solution:
---------
Using the econometrics toolbox here is an example where t ratio = 2.2000 and degree of freedom= 5 ; The computed two tailed P Value is 0.0791



tdis_prb
function y = tdis_prb(x,n)
% PURPOSE: calculates t-probabilities for elements in x-vector
%---------------------------------------------------
% USAGE: y = tdis_prb(x,n)
% where: x = vector containing computed t-values
%        n = degrees of freedom parameter
%---------------------------------------------------
% RETURNS:
%        y = a vector of marginal probability levels
% --------------------------------------------------
% SEE ALSO: fdis_prb(), chis_prb
%---------------------------------------------------

% written by:
% James P. LeSage, Dept of Economics
% University of Toledo
% 2801 W. Bancroft St,
% Toledo, OH 43606
% jpl@jpl.econ.utoledo.edu


if nargin ~= 2; error('Wrong # of arguments to tdis_prb'); end;
if n <=0; error('dof is negative or zero in tdis_prb'); end;

x2 = n./(n+x.^2);
one = find(x2 >= 1);
if length(one) > 0
    x2(one,1) = 1-1e-12;
end;
zip = find(x2 <= 0);
if length(zip) > 0
    x2(zip,1) = 1e-12;
end;

tmp = 1.0 - 0.5*betainc(x2,0.5*n,0.5);
y = 2*(1-tmp);
x
n
end
x =

    2.2000


n =

     5


ans =

    0.0791

Exercise:
-----------
Using this function (tdis_prb) plot results for [-4:0.05:4] for 10 degrees of freedom. 

Tuesday, December 13, 2011

In MATLAB: Sort an Array and then Undo that sort

PROBLEM:
--------------
Imagine I have an array to be sorted and after I sort it I want to UNDO that sort so that the array came back to the initial condition.

How?
---------
 A=[10 9 7 4 5 7 5 3 ];
>> [SORTED_A,IX]=sort(A)

SORTED_A =
     3     4     5     5     7     7     9    10
IX =
     8     4     5     7     3     6     2     1

>> UNSORTED=1:length(A)
UNSORTED =
     1     2     3     4     5     6     7     8

>> NEW_INDEX(IX)=UNSORTED
NEW_INDEX =
     8     7     5     2     3     6     4     1


>> NEW_A=SORTED_A(NEW_INDEX)
NEW_A =
    10     9     7     4     5     7     5     3

>> isequal(A,NEW_A)

ans =

     1