nam
nam

Reputation: 215

Design a simple matrix to group values from matrices

This problem is a succession of my previous problem:

1) Extract submatrices, 2) vectorize and then 3) put back

Now, I have two patients, named Ann and Ben.

enter image description here

Indeed the matrices A and B are data for Ann and the matrix C is data for Ben:

enter image description here

Now, I need to design a matrix M such that y = M*x where

y = [a11, a21, a12, a22, b11, b21, b12, b22]' which is a vector, resulting from concatenation of the top-left sub-matrices, Ann and Ben;

x = [2, 5, 4, 6, 7, 9, 6, 2, 9, 3, 4, 2]' which is a vector, resulting from concatenation of sub-matrices A, B and C.

Here, the M is a 8 by 12 matrix that

a11 = 2 + 7, a21 = 5 + 9, .., a22 = 6 + 2 and b11 = 9, ..b22 = 2.

I design the M manually by:

M=zeros(8,12)
M(1,1)=1; M(1,5)=1; % compute a11
M(2,2)=1; M(2,6)=1; % compute a21
M(3,3)=1; M(3,7)=1; % compute a12
M(4,4)=1; M(4,8)=1; % compute a22
M(5,9)=1; % for setting b11 = 9, C(1,1)
M(6,10)=1; % for setting b21 = 3, C(2,1)
M(7,11)=1; % for setting b12 = 4, C(1,2)
M(8,12)=1 % for setting b22 = 2, C(2,2)

Obviously, in general for M(i,j), i means the 8 linear-index position of vector y and j means linear-index position of vector x.

However, I largely simplified my original problem that I want to construct this M automatically.

Thanks in advance for giving me a hand.

Upvotes: 2

Views: 83

Answers (2)

Matt
Matt

Reputation: 2802

You can index into things and extract what you want without multiplication. For your example:

A = [2 4 8; 5 6 3; 10 3 6];
B = [7 6 3; 9 2 9; 10 2 3];
C = [9 4 7;3 2 5; 10 3 4];
idx = logical([1 1 0;1 1 0; 0 0 0]);
Ai = A(idx);
Bi = B(idx);
Ci = C(idx);
output = [Ai; Bi; Ci];
y = [Ai + Bi; Ci]; % desired y vector

This shows each step individually but they can be done in 2 lines. Define the index and then apply it.

idx = logical([1 1 0;1 1 0;0 0 0]);
output = [A(idx); B(idx); C(idx)];
y = [Ai + Bi; Ci]; % desired y vector

Also you can use linear indexing with idx = [1 2 4 5]' This will produce the same subvector for each of A B C. Either way works.

idx = [1 2 4 5]';

or alternatively

idx = [1;2;4;5];
output = [A(idx); B(idx); C(idx)];
y = [Ai + Bi; Ci]; % desired y vector

Either way works. Check out doc sub2ind for some examples of indexing from MathWorks.

Upvotes: 1

josoler
josoler

Reputation: 1423

Here you have my solution. I have essentially build the matrix M automatically (from the proper indexes) as you suggested.

A = [2 4 8;
    5 6 3;
    10 3 6];

B = [7 6 3;
    9 2 9;
    10 2 3];
C = [9 4 7;
    3 2 5;
    10 3 4];

% All matrices in the same array
concat = cat(3, A, B, C);
concat_sub = concat(1:2,1:2,:);
x = concat_sub(:);

n = numel(x)/3; %Number of elements in each subset
M2 = zeros(12,8); %Transpose of the M matrix (it could be implemented directly over M but that was my first approach)

% The indexes you need
idx1 = 1:13:12*n; % Indeces for A
idx2 = 5:13:12*2*n;   % Indices for B and C


M2([idx1 idx2]) = 1;
M = M2';

y = M*x

I have taken advantage of the shape that the matrix M shold take: enter image description here

Upvotes: 1

Related Questions