Path: news.mathworks.com!not-for-mail
From: "Tom Lane" <tlane@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: classify in 3D
Date: Mon, 2 Nov 2009 18:02:10 -0500
Organization: The MathWorks, Inc
Lines: 47
Message-ID: <hcnodi$aob$1@fred.mathworks.com>
References: <hchee8$45d$1@fred.mathworks.com>
Reply-To: "Tom Lane" <tlane@mathworks.com>
NNTP-Posting-Host: lanet.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1257202930 11019 172.31.57.151 (2 Nov 2009 23:02:10 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 2 Nov 2009 23:02:10 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5843
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:581926


> I have data similar to that of the fisheriris data in the classification 
> demo
. . .
> How would I extend the formula for 3 variables ? It seems that the order 
> of the coefficients provided by classify is not further documented in 
> detail.

Thomas, the example picks apart the coefficient arrays into separate scalars 
so they can be conveniently used in a two-dimensional example. The intent is 
that the coefficients are documented using the following matrix-oriented 
expression given in the help:

    0 < K + x*L + x*Q*x'

To plot this, the isosurface function may be useful. I don't know of an 
easier way. Try this:

% Get 3 measurements from 2 iris species
load fisheriris
rows = 51:150;
X = meas(rows,1:3);
x = X(:,1); y = X(:,2); z = X(:,3);
s = species(rows);
a = 1:50; b = 51:100;

% Do quadratic classification and get coefficients of boundary
[c,err,post,logl,str] = classify(X,X,s,'quadratic');
K = str(1,2).const;
L = str(1,2).linear;
Q = str(1,2).quadratic;

% Plot the data and curve K + [x,y,z]*L + [x,y,z]*Q*[x,y,z]' = 0:
xv = linspace(4.5,8,10); % vectors to cover the range of each column
yv = linspace(2,4,10);
zv = linspace(2.5,7,10);
[xx,yy,zz] = meshgrid(xv,yv,zv);
f = @(x,y,z) K + [x y z]*L + sum([x y z] .* ([x y z]*Q), 2);
v = f(xx(:),yy(:),zz(:));
v = reshape(v,size(xx));
plot3(x(a),y(a),z(a),'rv', x(b),y(b),z(b),'b^');
hold on
isosurface(xx,yy,zz,v,0);
hold off

-- Tom