mardi 7 novembre 2017

Matlab matching two matrices of different size based on condition

I have two matrices, A of size 1x30974 and B of size 55x30974. Matrix A contains values from 1 to to 30974, while matrix B (first row) contains values that are also elements of matrix A, yet they do not have to be in order.

So in a simple case, I would have:

A = [1 2 3 4 5 6 7 8]
B = [1 2 6 8; 20 21 22 23; 30 31 32 33]

I would like to compare A and B in a way that, my output would return:

C = [1 2 3 4 5 6 7 8; 20 21 NaN NaN NaN 22 NaN 23; 30 31 NaN NaN NaN 32 NaN 33]

Saying differently, if the value in B is an element of A, then return all values in this column. If B is not an element of A, then return NaN.

In my case, the output would be of size 55x30974.

I guess that ismember could be the function I am looking for, but even then, how could I get the values of the column?

Aucun commentaire:

Enregistrer un commentaire