I don't understand why for test suite 4 :
a=[0 1 1 1 0 2 2 0 1 1 1 0];
y_correct = [1 1];
I expect y_correct = [1 1 1]
The idea seems to be to return the element that occurs in the longest run, or all such elements in case of a tie. In case 4 there are two longest runs, both with element 1.
Thanks Tim for the explanation . So as the vector [1 1 1] appears twice in test 4 with the unique value 1 , the result must be twice the unqiue value -> [ 1 1]. ok ok thanks again
Nice
I have this array as I counted the times it repeats. However, can someone give me a command to pull out the max values with the number to the left? Any advise? The first value in column on the right top doesn't matter as it should always be 1. I try the max command it only shows the max value of 3...:
1 0
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Nice
Great problem. Thank you.
Good one
Nice problem
Is there a way to test output without submitting?
function val=longrun(a)
a_len=length(a); %计算a的长度
c=ones(a_len,1); %初始化对应位置的连续次数
for num_a=1:(a_len-1)
n_val=1;
for next_a=(num_a+1):a_len
if a(num_a)==a(next_a) %如果这个数字和后面的数字相等，则n_val+1，并进行下一次循环
n_val=n_val+1;
c(num_a)=n_val;
else
c(num_a)=n_val; %如果这个数字和后面数字不等，则把当前的n_val赋值给对应数字的连续次数，并跳出内层for循环
break
end
end
end
a_conti=max(c); %找出c数组中最大的连续次数
max_location=find(c==a_conti); %对应最大连续次数的位置
val=a(max_location); %找出对应在a中的位置
end
Test 1 and 4 are wrong
find(vertcat(1,diff(a(:)),1));
val=a(ans(diff(ans)==max(diff(ans))))
@Piero Cimule
Could you please comment on this method? It is very unlikely for me as a beginner in Matlab to have such a concise code. You are more than welcome to elaborate here or via my e-mail address zasevzasev42@gmail.com
Could you check this for me, please?
[ra,ca]=size(a);
if ra>ca
a=a';
end
j=1;
V=[a(1,1);1];
for i=2:size(a,2)
X=a(1,i);
if V(1,j)==X
V(2,j)=V(2,j)+1;
else
j=j+1;
V(1,j)=X;
V(2,j)=V(2,j)+1;
end
end
val=V(V(2,:)==max(V(2,:)))
if ra>ca
val=val';
end
How else could you solve in any other traditional programming language?
It was not so easy...
What kind of solution is that?
that's a bug of Matlab Cody, the use of ! and regexp.
I don't know why Mathworks has it not corrected.
Can't understand a bit of this code.
i'd really like to see a better solution that mine, one that uses difference equations..mine is size 129 and is just a control loop. Guess i've got to search in solution map.
These are non-constructive solutions and should be removed.
function ans=longrun(a)
d = find([true;diff(a(:))~=0;true]);
f = [d(1:end-1) diff(d)];
a(f(f(:,2)==max(f(:,2))))
end
very clever!
Make the vector [1 2 3 4 5 6 7 8 9 10]
44166 Solvers
19456 Solvers
2026 Solvers
370 Solvers
MATCH THE STRINGS (2 CHAR) very easy
255 Solvers