MATLAB: How to generate a matrix

matrix

Hi, I have two array and I want to generate one matrix according to these arrays.
For example:
a={2,5,2,4,2,3,4,4,8}
b={1,3,1,3,4,2,3,3,2}
Matrix is 2 to 1 -> 1, 5 to 3 -> 1, 2 to 1 -> 2 (again) …
Output:
Thanks for your helps.

Best Answer

  • Hi!
    Loop over your a/b vectors and increase the specified matrix element:
    M = zeros(max(a), max(b));
    for n = 1:length(a)
    M(a(n), b(n)) = M(a(n), b(n)) + 1;
    end