Find a value in a vector

2 visualizaciones (últimos 30 días)
Mihaela Bocai
Mihaela Bocai el 6 de Jun. de 2021
Comentada: Mihaela Bocai el 7 de Jun. de 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!

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jun. de 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
  1 comentario
Mihaela Bocai
Mihaela Bocai el 7 de Jun. de 2021
Thank you for helping me! I understand what the code is doing.
In my question, a give it an simple example, a vector with 13 positions, but in reality I need for a vector with 744 positions (can apply you code for such a long vector? I am asking because the results that Matlab will give it to me it will be very long and I have to count too many times what number after my pattern was repeated ), reasons for why I said that 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.
Then, I want to obtain directly this number like a result from Matlab >>FinalPrediction=33 , without search in a such long the table that matlab will give it like result. I want that Matlab give it to mine the next prediction that I will have in the next hour.
This vector will contain values from energy consumed by household consumers every hour, I have a history with this values and I want to predict the te energy consumed in the next hour.
Sorry, I don't write very well in english, I hope that you've undestood me?
I've tried you code and Matlab gives me:
I don't understand why Matlab returns me that many rows with indexes(from what I know the index is used to find the position Where is your value that you are searching, but I don't need that Matlab gives me the position of the Pattern but the next value after the Pattern) and what is the meaning of the row "indexes = [] " ??
Pattern = 25
us = 25 33 56 68 98
indexes = []
indexes = 1 6 8
indexes = []
indexes = 10
indexes = 4 12
indexes = 7 9
indexes = []
indexes = 2
indexes = []
indexes = []
indexes = 3
indexes = []
indexes = []
indexes = []
indexes = []
indexes = 11
indexes = []
indexes = []
indexes = []
indexes = []
indexes = 5
indexes = []
indexes = []
indexes = []
indexes = []
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
Sorry for my many questions! Thank you,very, very much!

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 6 de Jun. de 2021
V = [ .......] %
Most_V = mode(V);
VFinal = [V, mode(V)];

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by