Replacing numbers around a specific index(CAN'T FIGURE IT OUT)
Show older comments
I am trying to create a sub-function for a EEG analysis program. Basically the data is a 1x892 matrix of data with numbers ranging from 1 to 99. This portion that i am trying to deal with has two parts:
1. Go through the data and find the value 9, whenever it occurs in the 1x892 matrix. I have already done this part.
2. Replace the numbers surrounding the identified number 9. So if there was data with 9 in it follows, [... a b c d 9 e f g h i j k...]. Basically after the part of identifying the number 9, the letters a through d need to be replaced with 5,6,7,8 respectively. The letters e through k need to be replaced with 10,11,12,13,14,15,16, respectively. I am struck on this part and i would really appreciate any help.
P.s, the way this data is arranged, that we get from an EEG, a 9 will not overlap with another 9.
4 Comments
@Sanwal Yousaf: Why did you delete your other identical question, where I had already provided you with a simple working example of how you can achieve this?:
I notice that you paid attention to the questions that I asked, as these are now explained in your new question.
Did my answer not work on your computer?
Sanwal Yousaf
on 6 Aug 2015
Sanwal Yousaf
on 6 Aug 2015
Stephen23
on 14 Aug 2015
Accepted Answer
More Answers (1)
It can be done very easily by converting the integers to char and using regepxrep:
>> A = repmat(99:-1:1,1,9); % fake data vector
>> Z = +regexprep(char(A),sprintf('.{0,4}%c.{0,7}',9),char(5:16));
>> A(82:102)
ans =
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 99 98 97
>> Z(82:102)
ans =
18 17 16 15 14 5 6 7 8 9 10 11 12 13 14 15 16 1 99 98 97
3 Comments
Sanwal Yousaf
on 6 Aug 2015
Sanwal Yousaf
on 6 Aug 2015
Most likely this occurs because you have some nines that are too close to each other, so that the replaced values overlap: in this case the output vector will be of a different size to the input vector and some values will be repeated. This is why I asked in your original question (that you deleted) specifically about the overlap of the nines: how close can they be?
Unfortunately you do not provide any sample data for us to try, so it is impossible for me to know why this happens with your data sets. I am happy to invent my own data set, but this is likely going to have quite different properties to the data that you are working with. Indeed you can see that I invented a data vector where the nines are spread very far apart, so there in no problem with overlap this case. If there is some overlap, then you need to define how the output is specified: do the leading or trailing digits have priority, or do we ignore overlapping nines?
I am happy to help you and show you how to make this work, but I will need your data to try out.
Categories
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!