<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372</link>
    <title>MATLAB Central Newsreader - 3D matrix operation speed up</title>
    <description>Feed for thread: 3D matrix operation speed up</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 12 Feb 2009 16:28:02 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627921</link>
      <author>us</author>
      <description>&quot;francesco santi&quot;&lt;br&gt;
&amp;gt; I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component...&lt;br&gt;
&lt;br&gt;
one of the many solutions&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m=ceil(10*rand(2,4,3));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;v=[10,100,1000];&lt;br&gt;
% the engine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=arrayfun(@(x) m(:,:,x)-v(x),1:3,'uni',false);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=cat(3,r{:});&lt;br&gt;
% the result&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(r);&lt;br&gt;
%{&lt;br&gt;
(:,:,1) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-7    -2    -9    -2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-3    -2    -1    -1&lt;br&gt;
(:,:,2) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-94   -92   -96   -97&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-97   -98   -90   -96&lt;br&gt;
(:,:,3) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;-991  -997  -990  -994&lt;br&gt;
&amp;nbsp;&amp;nbsp;-996  -994  -993  -992&lt;br&gt;
%}&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Thu, 12 Feb 2009 17:28:02 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627935</link>
      <author>francesco santi</author>
      <description>&amp;gt; is this the loop you used?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for n = 1:3&lt;br&gt;
&amp;gt;     im(:,:,n) = im(:,:,n)-vec(n);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; then maybe you can accelerate it with imsubtract if you have the image processing toolbox&lt;br&gt;
&lt;br&gt;
% this generates the cell which contains the image&lt;br&gt;
HSV = hsv2rgb(im);&lt;br&gt;
ImageMatrix = cell(HSV_rows, HSV_cols);&lt;br&gt;
for r=1:HSV_rows&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for c=1:HSV_cols&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ImageMatrix{r,c} = [HSV(r,c,1) HSV(r,c,2) HSV(r,c,3)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
%this is the loop to compute the subtraction between a pixel and its mean&lt;br&gt;
for r=1:HSV_rows&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for c=1:HSV_cols&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fl = double(cell2mat(ImageMatrix(r,c))); %pixel color components 1x3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp(r,c) = (fl - Ml); %Ml=mean vector 1x3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Thu, 12 Feb 2009 19:36:01 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627960</link>
      <author>francesco santi</author>
      <description>Thanks us for the detailed and quick reply.&lt;br&gt;
Could you please have a look to the code that I have posted and formulate another &quot;ad hoc&quot; solution with cell?&lt;br&gt;
Maybe with similar size and variables name to make it easier to understand.&lt;br&gt;
Thank you again for your time and help.</description>
    </item>
    <item>
      <pubDate>Mon, 16 Feb 2009 22:16:01 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628707</link>
      <author>francesco santi</author>
      <description>Thanks &quot;us&quot; for the detailed and quick reply.&lt;br&gt;
Could you please have a look to the code that I have posted and formulate another &quot;ad hoc&quot; solution with cell?&lt;br&gt;
Maybe with similar size and variables name to make it easier to understand.&lt;br&gt;
Sorry but Matlab help is quite short and not very clear, for me, about this function.&lt;br&gt;
Thank you again for your time and help. </description>
    </item>
    <item>
      <pubDate>Tue, 17 Feb 2009 09:32:01 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628793</link>
      <author>nor ki</author>
      <description>&quot;francesco santi&quot; &amp;lt;fpsanti@gmail.com&amp;gt; wrote in message &amp;lt;gn1m72$r6k$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; is this the loop you used?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for n = 1:3&lt;br&gt;
&amp;gt; &amp;gt;     im(:,:,n) = im(:,:,n)-vec(n);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; then maybe you can accelerate it with imsubtract if you have the image processing toolbox&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % this generates the cell which contains the image&lt;br&gt;
&amp;gt; HSV = hsv2rgb(im);&lt;br&gt;
&amp;gt; ImageMatrix = cell(HSV_rows, HSV_cols);&lt;br&gt;
&amp;gt; for r=1:HSV_rows&lt;br&gt;
&amp;gt;     for c=1:HSV_cols&lt;br&gt;
&amp;gt;         ImageMatrix{r,c} = [HSV(r,c,1) HSV(r,c,2) HSV(r,c,3)];&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; %this is the loop to compute the subtraction between a pixel and its mean&lt;br&gt;
&amp;gt; for r=1:HSV_rows&lt;br&gt;
&amp;gt;     for c=1:HSV_cols&lt;br&gt;
&amp;gt;         fl = double(cell2mat(ImageMatrix(r,c))); %pixel color components 1x3&lt;br&gt;
&amp;gt;         temp(r,c) = (fl - Ml); %Ml=mean vector 1x3&lt;br&gt;
&amp;gt;      end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Hi Francesco,&lt;br&gt;
work on whole matrices, for example with the solution of 'us'  or with the loop i proposed. Changing names should be your business. The matlab help is really nice indeed, have fun.&lt;br&gt;
&lt;br&gt;
kinor</description>
    </item>
    <item>
      <pubDate>Tue, 17 Feb 2009 10:15:03 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628798</link>
      <author>francesco santi</author>
      <description>Thanks again for the answer.&lt;br&gt;
Sorry, but I didn't want someone to do my job. :)&lt;br&gt;
I am quite new to Matlab and for me the Matlab help seems not so easy/clear.&lt;br&gt;
I tried the arrayfun solution and it works, but not as i would like to.&lt;br&gt;
It was my fault because I couldn't explain the problem very well!&lt;br&gt;
I'll try again.&lt;br&gt;
&lt;br&gt;
I have a MxNx3 matrix which is an image.&lt;br&gt;
I have to compute the following formula for each pixel and store the result in a MxN matrix.&lt;br&gt;
Singleton(row,col) = (px(row,col)-m(class)) * invS(class) * (px(row,col)-m(class))';&lt;br&gt;
&lt;br&gt;
px=RGB_matrix(row,col,:):&lt;br&gt;
px=reshape(px,1,3); %I have the 3 RGB color components of one pixel in one row&lt;br&gt;
m=[mean(R) mean(G) mean(B)]; %the mean vector contains the mean value of the color components&lt;br&gt;
invS %is the inverse of the covariance matrix&lt;br&gt;
&lt;br&gt;
The mean vector and inverse matrix change depending on the class which the pixel belongs to. So I also wrote a for loop to compute all the m and invS belonging to each class.&lt;br&gt;
&lt;br&gt;
I hope the problem is more clear now.&lt;br&gt;
I have to compute that formula over all the pixels trying to avoid for loops. More over I have to store the result in a new MxN matrix in the same position of the original pixel.&lt;br&gt;
Now I am using 3 for loops and for big images it is very slow, of course.</description>
    </item>
    <item>
      <pubDate>Tue, 17 Feb 2009 10:20:05 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628799</link>
      <author>francesco santi</author>
      <description>this is the detailed function that i am using. &lt;br&gt;
I hope it help you to understand better my problem.&lt;br&gt;
&lt;br&gt;
function Singleton = singleton(classes, Parameters)&lt;br&gt;
global HSV_rows HSV_cols HSV&lt;br&gt;
%HSV is the image matrix (RGB-&amp;gt;HSV color space)&lt;br&gt;
&lt;br&gt;
Singleton = cell(1,classes);&lt;br&gt;
for l=1:classes&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp = zeros(HSV_rows, HSV_cols);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;detSl = Parameters(l).DetermS; %determinant&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp2 = -log(1/(sqrt(power(2*pi,3)*detSl)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Ml = Parameters(l).Med; %mean value&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;invSl = Parameters(l).InverS; %inverse covariance matrix&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for r=1:HSV_rows&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for c=1:HSV_cols&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fl = reshape(HSV(r,c,:),1,3); %color components of pixel in r,c&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;temp(r,c) = (fl - Ml) * invSl * (fl - Ml)';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Singleton{l} = temp2+temp*.5; %storing the result here&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Tue, 17 Feb 2009 10:32:02 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628803</link>
      <author>Jos </author>
      <description>&quot;francesco santi&quot; &amp;lt;fpsanti@gmail.com&amp;gt; wrote in message &amp;lt;gne2n7$5f2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thanks again for the answer.&lt;br&gt;
&amp;gt; Sorry, but I didn't want someone to do my job. :)&lt;br&gt;
&amp;gt; I am quite new to Matlab and for me the Matlab help seems not so easy/clear.&lt;br&gt;
&amp;gt; I tried the arrayfun solution and it works, but not as i would like to.&lt;br&gt;
&amp;gt; It was my fault because I couldn't explain the problem very well!&lt;br&gt;
&amp;gt; I'll try again.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a MxNx3 matrix which is an image.&lt;br&gt;
&amp;gt; I have to compute the following formula for each pixel and store the result in a MxN matrix.&lt;br&gt;
&amp;gt; Singleton(row,col) = (px(row,col)-m(class)) * invS(class) * (px(row,col)-m(class))';&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; px=RGB_matrix(row,col,:):&lt;br&gt;
&amp;gt; px=reshape(px,1,3); %I have the 3 RGB color components of one pixel in one row&lt;br&gt;
&amp;gt; m=[mean(R) mean(G) mean(B)]; %the mean vector contains the mean value of the color components&lt;br&gt;
&amp;gt; invS %is the inverse of the covariance matrix&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The mean vector and inverse matrix change depending on the class which the pixel belongs to. So I also wrote a for loop to compute all the m and invS belonging to each class.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I hope the problem is more clear now.&lt;br&gt;
&amp;gt; I have to compute that formula over all the pixels trying to avoid for loops. More over I have to store the result in a new MxN matrix in the same position of the original pixel.&lt;br&gt;
&amp;gt; Now I am using 3 for loops and for big images it is very slow, of course.&lt;br&gt;
&lt;br&gt;
Did you look at the PROFILER to see the bottlenecks of your code?&lt;br&gt;
&lt;br&gt;
help profile&lt;br&gt;
&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Tue, 17 Feb 2009 12:23:01 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#628831</link>
      <author>francesco santi</author>
      <description>yes, i am using the profiler and the most used and time consuming function is that &quot;singleton&quot;</description>
    </item>
    <item>
      <pubDate>Thu, 12 Feb 2009 16:03:01 -0500</pubDate>
      <title>3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627910</link>
      <author>francesco santi</author>
      <description>Hello, I have the following problem.&lt;br&gt;
I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component.&lt;br&gt;
I have a 1x3 vector which contains the mean value of the R G B components.&lt;br&gt;
I've done it with a for loop but it's very slow for big images (i.e. matrix) and I'm pretty sure there's a faster way to do it.&lt;br&gt;
I've tried also to put all the 3 RGB components in a cell, so I have in position r,c a vector of 3 color components. Then I am using a for loop and cell2mat to do the same operation as above. But again I need to replace the for loop for speed.&lt;br&gt;
Any help would be appreciated.&lt;br&gt;
Thanks in advance,&lt;br&gt;
Francesco</description>
    </item>
    <item>
      <pubDate>Thu, 12 Feb 2009 16:11:01 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627914</link>
      <author>nor ki</author>
      <description>&quot;francesco santi&quot; &amp;lt;fpsanti@gmail.com&amp;gt; wrote in message &amp;lt;gn1h7l$qs0$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello, I have the following problem.&lt;br&gt;
&amp;gt; I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component.&lt;br&gt;
&amp;gt; I have a 1x3 vector which contains the mean value of the R G B components.&lt;br&gt;
&amp;gt; I've done it with a for loop but it's very slow for big images (i.e. matrix) and I'm pretty sure there's a faster way to do it.&lt;br&gt;
&amp;gt; I've tried also to put all the 3 RGB components in a cell, so I have in position r,c a vector of 3 color components. Then I am using a for loop and cell2mat to do the same operation as above. But again I need to replace the for loop for speed.&lt;br&gt;
&amp;gt; Any help would be appreciated.&lt;br&gt;
&amp;gt; Thanks in advance,&lt;br&gt;
&amp;gt; Francesco&lt;br&gt;
&lt;br&gt;
Hello Francesco,&lt;br&gt;
&lt;br&gt;
is this the loop you used?&lt;br&gt;
&lt;br&gt;
for n = 1:3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;im(:,:,n) = im(:,:,n)-vec(n);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
then maybe you can accelerate it with imsubtract if you have the image processing toolbox&lt;br&gt;
cheers&lt;br&gt;
kinor</description>
    </item>
    <item>
      <pubDate>Thu, 12 Feb 2009 16:15:05 -0500</pubDate>
      <title>Re: 3D matrix operation speed up</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/244372#627917</link>
      <author>us</author>
      <description>&quot;francesco santi&quot;&lt;br&gt;
&amp;gt; I have a MxNx3 matrix (an image) and I would like to subtract to each pixel the mean value of each color component...&lt;br&gt;
&lt;br&gt;
one of the many solutions&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m=ceil(10*rand(2,4,3));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;v=[10,100,1000];&lt;br&gt;
% the engine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=arrayfun(@(x) m(:,:,x)-v(x),1:3,'uni',false);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=cat(3,r{:});&lt;br&gt;
% the result&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(r);&lt;br&gt;
%{&lt;br&gt;
(:,:,1) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-7    -2    -9    -2&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-3    -2    -1    -1&lt;br&gt;
(:,:,2) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-94   -92   -96   -97&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;-97   -98   -90   -96&lt;br&gt;
(:,:,3) =&lt;br&gt;
&amp;nbsp;&amp;nbsp;-991  -997  -990  -994&lt;br&gt;
&amp;nbsp;&amp;nbsp;-996  -994  -993  -992&lt;br&gt;
%}&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
  </channel>
</rss>

