Link corresponding elements of two matrices of the same dimension

1 view (last 30 days)
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 Comments

Sign in to comment.

Answers (1)

Adam
Adam on 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 Comments
jasmine
jasmine on 9 Jan 2019
A = blk.visDiff(:,1);
B = blk.responseMade(:,1);
% these 2 are my matrices (or could be merged as 1 with 2 columns)
And I want to count for each element of A the number of times the corresponding element in B is 1, and than divide the number calculated by the total times when A is equal to each specific value.
For example:
calculate how many times A = 100 and B = 1 --> and then divide this number by the number of times A = 100
I always need to calculate this for B=1, but for multiple values of A
Adam
Adam on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!