1

Couldn't find a quick answer for this, it seems super simple but I don't get it. I want do the following transformation (for this example using my imaginary function transform):

a=[0 0 0 1 0 0 0 0];
b=(-1:2); %rule to transform for every true value in [a], -1:2 should be true
transform(a,b) %should output [0 0 1 1 1 1 0 0]

a=[0 0 0 1 0 0 0 0 1 1 0 0 0]; %another example
transform(a,b) %should output [0 0 1 1 1 1 0 1 1 1 1 1 0];

Is there an quick way of doing this transform, maybe using logical operators?

edit: I tried

a(find(a)'+(-1:2))=1 %requires matlab>2016 if I'm not mistaken, otherwise replace + sign with bsxfun(@plus,...)

but I'm looking for a possible function that does this without changing a and without using find (since using find kind of defeats the purpose of using logical matrices/indexing in the first place)

user2305193
  • 2,079
  • 18
  • 39

2 Answers2

2

I found an elegant oneliner that should solve your problem:

b=(-1:2)
a(find(a) + b(:)) = 1;

Hope it helps!

Sebastian Dengler
  • 1,258
  • 13
  • 30
2

If you have the Image Processing Toolbox you can use imdilate:

nh(max(abs([b(1),b(end)]))+1+b) = true;
result = imdilate(a, nh);
rahnema1
  • 15,264
  • 3
  • 15
  • 27