Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Optimize this matrix process (alternative to sub2ind)
Date: Sat, 16 Oct 2010 16:30:07 +0000 (UTC)
Organization: Ryerson University
Lines: 42
Message-ID: <i9cjuf$7qf$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1287246607 8015 172.30.248.38 (16 Oct 2010 16:30:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 16 Oct 2010 16:30:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1071669
Xref: news.mathworks.com comp.soft-sys.matlab:678999

Hello guys,

I thought my problem below is solved using sub2ind however it appears to be time consuming. It cost me 10 sec :S

Here is the bunch of the code that shows how many times sub2ind i am using. Based on the profile report, each line is iterated for about 59000 times. Also A in my case is a matrix of size 24x24...

Hence using this code, these sub2ind lines take around 10 seconds :S:S

Any help in optimizing this code is highly appreciated

A=[1 2 3 4 5;
   6 7 8 9 10;
   11 12 13 14 15;
   16 17 18 19 20];

x=[1;2;3]; % some desired 'x' index of matrix A
y=[2;4;4]; % some desired 'y' index of matrix A

fy = floor(y);
cy = ceil(y);
rx = round(x);
ry = round(y);
fx = floor(x);
cx = ceil(x);

ANDED=[1;0;1]; % obtained using some operation
% what I need is when ANDED=1, take the pixel of A(x,y) and do the operation and when
% ANDED=0, take the corresponding A(x,y) and do other operation

N(ANDED==1)=A(sub2ind(size(A),ry(ANDED==1),rx(ANDED==1)));
     
N(ANDED==0) = A(sub2ind(size(A),fy(ANDED==0),fx(ANDED==0))) + ...
               A(sub2ind(size(A),fy(ANDED==0),cx(ANDED==0))) + ...
               A(sub2ind(size(A),cy(ANDED==0),fx(ANDED==0))) + ...
               A(sub2ind(size(A),cy(ANDED==0),cx(ANDED==0)));


p.s. sorry for re-posting this question but it was blended in my previous post :)

regards,

Louis