Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Give me a good example for accumarray()
Date: Sat, 4 Jul 2009 18:32:00 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <h2o770$668$1@fred.mathworks.com>
References: <h2n5b9$lo7$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246732320 6344 172.30.248.35 (4 Jul 2009 18:32:00 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 4 Jul 2009 18:32:00 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1477385
Xref: news.mathworks.com comp.soft-sys.matlab:552797


Thanks for the responses. I'm trying to learn of it and of arrayfun (I'm good with cellfun but having troubles with arrayfun). I want to do this for example:

say I have a matrix s;

s =

     1     1
     6     1
     1     2
     5     2
     1     3
     2     3
     8     4
     1     7
     3     7
     7     7
     3     8
     8     8

I want to find the rows which have a similar first column and a second column that is different by +/- 1. 

For example for first column value (1) I will get

1 1
1 2
1 3

and for first column value (3)

3 7
3 8

I can do a loop but my one liner attempts have failed. I'm trying this but it's wrong. Can someone please tell me what I'm doing wrong and if there is a way using accumarray or arrayfun? Thanks!!!

% for first column value 1 I want second column values that are close
s(arrayfun(@(x,y)x==1 && sum(ismember(y(x==1),y-1:y+1)),s(:,1),s(:,2)),2)

Help!