Link corresponding elements of two matrices of the same dimension
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have two different fields in the same structure, each of them containing 1 column table with multiple rows (but same number of rows for both of them), in a mat file. Each tabel contains data from a lab experiment, each row represents a different trial, and each cell of the table is from the same trial as the corresponding one in the other table. How can I link each cell of the first table with its corresponding one in the second table in a program, so I can do something like this: Count how many times the value of the element in one of the rows of the first table is x and its corresponding value in the second table is y.
3 commentaires
Réponses (1)
Adam
le 9 Jan 2019
I never use tables myself, but something like the following should work fine:
t = table( randi(10, 20, 1 ), randi(10, 20, 1 ) );
f = @(x,y) x == 7 && y == 9
numOccurences = sum( table2array( rowfun( f, t ) ) );
In your case you would already have your table with 2 columns of you arrange it that way instead of two distinct tables. I just created one as an example. Likewise I just put 7 and 9 as example numbers for x and y.
If you want to count all occurrences of all combinations it is more complicated, but that isn't what you asked for so this works for a given x and y.
3 commentaires
Adam
le 9 Jan 2019
If you are using a sufficiently up to date version of Matlab you can convert them to tables using
doc array2table
I assumed you meant you already had your data in tables. You can do it with a matrix too of course, it is just a bit less neat since there is no functionality (like rowfun above) to automatically apply a function to rows of a matrix, at least not that I am aware of.
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!