Find a value in a vector
2 views (last 30 days)
Show older comments
Mihaela Bocai
on 6 Jun 2021
Commented: Mihaela Bocai
on 7 Jun 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 Comments
Accepted Answer
Image Analyst
on 6 Jun 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
More Answers (1)
Sulaymon Eshkabilov
on 6 Jun 2021
V = [ .......] %
Most_V = mode(V);
VFinal = [V, mode(V)];
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!