Counting First digit of a certain number

Hi, I am working with a large number of data to analyze if there are any failures in data which having some hard time manually looking at the data or even with graphs. So I was wondering if there is a way to count the first digit number out of table which then I just need to look into how many failures happened. Thanks in advance...
For example,
t = [0, 0; 0, 1; 0, 1; 1, 1; 1, 0; 0, 0; 0, 1]
t = 7×2
0 0 0 1 0 1 1 1 1 0 0 0 0 1
t2 = [1, 1; 0, 1; 0, 1; 0, 1; 1, 1; 0, 1; 1, 1]
t2 = 7×2
1 1 0 1 0 1 0 1 1 1 0 1 1 1
t3 = [0, 1, 1; 1, 0, 0; 1, 1, 0; 1, 0, 1; 1, 1, 0; 0, 0, 1; 1, 1, 1]
t3 = 7×3
0 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 1 1 1 1
then
T_count = [1, 2]
T_count = 1×2
1 2
T2_count = [3, 1]
T2_count = 1×2
3 1
T3_count = [2, 4, 3]
T3_count = 1×3
2 4 3

 Réponse acceptée

t1 = [0, 0; 0, 1; 0, 1; 1, 1; 1, 0; 0, 0; 0, 1];
t2 = [1, 1; 0, 1; 0, 1; 0, 1; 1, 1; 0, 1; 1, 1];
t3 = [0, 1, 1; 1, 0, 0; 1, 1, 0; 1, 0, 1; 1, 1, 0; 0, 0, 1; 1, 1, 1];
fh = @(m) sum(diff([0*m(1,:);m],1,1)>0,1);
fh(t1)
ans = 1×2
1 2
fh(t2)
ans = 1×2
3 1
fh(t3)
ans = 1×3
2 4 3

7 commentaires

Hi Stephen, this is an interesting approach that I was not aware of.
This code would work if the numbers are only 0 and 1 right?
Then what if the table is like this? Sorry for changes in data.
Column_1 = [ 0; 0; 0; 1; 1; 0; 1]
Column_1 = 7×1
0 0 0 1 1 0 1
%Where 0 is default and 1 is error
Column_2 = [ 1; 1; 5; 2; 2; 1; 2]
Column_2 = 7×1
1 1 5 2 2 1 2
%Where 1 is default and 5 and 2 is error
Column_3 = [ 1; 1; 1; 0; 0; 1; 1]
Column_3 = 7×1
1 1 1 0 0 1 1
%where 1 is default and 0 is error
T = [Column_1, Column_2, Column_3]
T = 7×3
0 1 1 0 1 1 0 5 1 1 2 0 1 2 0 0 1 1 1 2 1
T_count = [2, 3, 1]
T_count = 1×3
2 3 1
M = [0, 1, 1; 0, 1, 1; 0, 5, 1; 1, 2, 0; 1, 2, 0; 0, 1, 1; 1, 2, 1]
M = 7×3
0 1 1 0 1 1 0 5 1 1 2 0 1 2 0 0 1 1 1 2 1
D = [0,1,1] % default for each column
D = 1×3
0 1 1
N = sum(diff([0*D;M-D],1,1) & M~=D, 1)
N = 1×3
2 3 1
Min
Min le 12 Mar 2024
Hi Stephen, this looks great!
I am planning to modify my code to see if this can solve my problem.
Thank you!
Min
Min le 13 Mar 2024
Hi Stephen, I have been making some modification to the code to make it work but it is keep saying this error.
Undefined function 'mtimes' for input arguments of type 'table'.
Any idea what that mtimes is coming from?
Stephen23
Stephen23 le 13 Mar 2024
Modifié(e) : Stephen23 le 13 Mar 2024
"Any idea what that mtimes is coming from?"
N = sum(diff([0*D;M-D],1,1) & M~=D, 1)
% ^ this is MTIMES
"I have been making some modification to the code to make it work but it is keep saying this error. Undefined function 'mtimes' for input arguments of type 'table'."
Apparently you are supplying data D as a table, whereas you should be supplying D as a numeric array. Perhaps you used the wrong type of indexing (i.e. parentheses instead of curly-braces):
Or perhaps you imported the data as a table, even though a numeric matrix would be quite sufficient. In any case, you need to fix that.
Min
Min le 13 Mar 2024
Oh! I see, yep my data was in a form of table since I was working with the timetable data.
I will go ahead and make some changes and see if I see any error. Thank you!
Min
Min le 13 Mar 2024
Hi Stephen, I tried out with some changes and it works great.
Thank you :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by