How to find and replace stand alone values in a logical array

4 views (last 30 days)
I have a logical aray, a 370x29 matrix. I am trying to find all 1s and 0s that are non-consecuitve and replace them with the other one, in each column. So in a series: 00000100000 I want to be able to find and replace that stand alone 1 with a 0, and in a series: 1111101111 I want to find and replace that 0 with a 1. So far, I have been able to find and replace these with this script, where a is my 370x29 matrix:
The only problem with this is that it also finds and replaces the first 1 in a string of 1s and the first 0 in a string of 0s.
Is there any way to find and replace these stand alone 1s and 0s?
Thanks :)

Accepted Answer

the cyclist
the cyclist on 19 Dec 2019
I think this does what you want:
a(strfind([1 a 1],[1 0 1])) = 1;
a(strfind([0 a 0],[0 1 0])) = 0;
Teddy Fisher
Teddy Fisher on 26 Feb 2020
Thanks! that totally works. i dont know how or why, but it does exactly what i was trying to

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!