Find a value in a vector
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mihaela Bocai
le 6 Juin 2021
Commenté : Mihaela Bocai
le 7 Juin 2021
Hello, I need some help.
I want to search in this vector s=[25 33 56 25 98 25 33 25 33 25 68 25 98], for this value, Pattern=25, and I want to memorize in a variable what numbers are repeated after this Pattern I choose and the one that is repeated most of the time to keep it in a variable FinalPrediction.
Pattern=25 => What is up next after Pattern :numbers.repeated= 33,98,33,33,68,98 => Results that number 33 is repeating 3 times after Pattern=25 That means my next number after the last number from s vector is number 33 .
>>FinalPrediction=33
>> s.final = [25 33 56 25 98 25 33 25 33 25 68 25 98, 33 ]
I don't now how to write a cod that gives to me this result
Thank you!
0 commentaires
Réponse acceptée
Image Analyst
le 6 Juin 2021
You can easily build up a table for how many times each possible combination of two numbers appears together (one after the other) in the string:
s=[25 33 56 25 98 25 33 25 33 25 68 25 98]
Pattern=25
us = unique(s)
numNumbers = length(us)
results = zeros(numNumbers, 3); % Number1, number2, # times this combination appears
loopCounter = 1;
for k1 = 1 : numNumbers
for k2 = 1 : numNumbers
indexes = strfind(s, [us(k1), us(k2)])
results(loopCounter, 1) = us(k1);
results(loopCounter, 2) = us(k2);
results(loopCounter, 3) = length(indexes);
loopCounter = loopCounter + 1;
end
end
% Show the results:
results
You get:
results =
25 25 0
25 33 3
25 56 0
25 68 1
25 98 2
33 25 2
33 33 0
33 56 1
33 68 0
33 98 0
56 25 1
56 33 0
56 56 0
56 68 0
56 98 0
68 25 1
68 33 0
68 56 0
68 68 0
68 98 0
98 25 1
98 33 0
98 56 0
98 68 0
98 98 0
Plus de réponses (1)
Sulaymon Eshkabilov
le 6 Juin 2021
V = [ .......] %
Most_V = mode(V);
VFinal = [V, mode(V)];
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!