Switch Case when a value stagnates
1 次查看(过去 30 天)
显示 更早的评论
Suppose I have a switch case scenario and a variable, var which records a value from a for-loop as such:
1st loop: var=10
2nd loop: var=9
3rd loop: var=9
4th loop: var=8
5th loop: var=8
6th loop: var=8
7th loop: var=8
8th loop: var=8
9th loop: var=8
When any value (in this case 8) is repeated for 3 times, I need to switch from case 1 to case 2. Now the question is, how can I check if a value is repeated 3 times?
In the case above, the first switch is performed when the value 8 is repeated 3 times (4th-6th iteration); and the second switch is performed the next series of value 8 is repeated for 3 times (7th-9th iteration).
0 个评论
采纳的回答
Ben11
2014-6-23
编辑:Ben11
2014-6-23
Here is a simple solution to detect the first occurence of values repeated 3 times; you could customize it to be more general.
clear all clc
A = [2 6 8 2 8 8 3 2 9 8 8 11 12 18 17 18 18 18 15 3];% Create a dummy vector
CheckSimilar = 0;
for i = 2:length(A)
if A(i) == A(i-1)
CheckSimilar = CheckSimilar +1;
end
if CheckSimilar == 3
fprintf('The value %i is repeated %i times',A(i),CheckSimilar);
return
end
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Handles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!