Find a value in a vector
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2021-6-6
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
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!