Replacing numbers around a specific index(CAN'T FIGURE IT OUT)
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sanwal Yousaf
le 6 Août 2015
Commenté : Stephen23
le 14 Août 2015
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.
Réponse acceptée
Star Strider
le 6 Août 2015
Modifié(e) : Star Strider
le 6 Août 2015
This seems to work:
x = randi(99, 1, 200); % Create Data
ix9 = find(x == 9);
for k1 = 1:length(ix9)
if (ix9(k1)+7) <= length(x) && (ix9(k1)-4) >= 1 % Check For Out-Of-Range Indices
ixrng = ix9(k1) + [-4:7]; % Calculate Index Range
x(ixrng) = 5:16; % Substitute
end
end
2 commentaires
Star Strider
le 6 Août 2015
That’s not a line from my code, so I have no idea.
I would use a function form of my code (saving it in your MATLAB path as a separate function file called EEGsubstitute.m and do the substitution as a separate step:
function EEGout = EEGsubstitute(EEGin)
EEGout = EEGin;
ix9 = find(EEGout == 9);
for k1 = 1:length(ix9)
if (ix9(k1)+7) <= length(EEGout) && (ix9(k1)-4) >= 1 % Check For Out-Of-Range Indices
ixrng = ix9(k1) + [-4:7]; % Calculate Index Range
EEGout(ixrng) = 5:16; % Substitute
end
end
end
Then call it as:
EEGout = EEGsubstitute(EEGin);
You will likely have to assign ‘EEGin’ from your original data or your structure first.
Note that you specified a row vector in your Question, so my code assumes the data will always be a row vector.
Plus de réponses (1)
Stephen23
le 6 Août 2015
Modifié(e) : Stephen23
le 6 Août 2015
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 commentaires
Stephen23
le 7 Août 2015
Modifié(e) : Stephen23
le 7 Août 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.
Voir également
Catégories
En savoir plus sur EEG/MEG/ECoG dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!