Message Boards Message Boards

GROUPS:

Matrix Operations in Mathematica

Posted 12 days ago
429 Views
|
7 Replies
|
0 Total Likes
|

I want to construct a M X N matrix with results imported from another matrix. The entries in this matrix shall be c1+c2,c1+c3,...c1+c22, c2+c3, c2+c4..... and so on. Basically, I want a matrix of all possible combinations. The problem with the attached code is probably in the partition line function as it only produces two pairs of columns with offset of one and not all the possible combinations of columns. Could you please help me solve this problem?

Attachments:
7 Replies

Nehal,

I am not sure exactly what you want to do. Maybe post a simple example of a 3x4 input and the output you expect. I would look at the function Inner. I think it can be coaxed to give you what you want without writing all of that code. For example:

mat = {{a1, a2, a3, a4}, {b1, b2, b3, b4}, {c1, c2, c3, c4}}
Inner[Plus, Transpose[mat], mat]

Returns a matrix which is {{col1+col1, col1+col2, col1+col3, col1+col4}, {{col2+col1, col2+col2, col2+col3, col2+col4}, {col3+col1, col3+col2, col3+col3, col3+col4}, {col4+col1, col4+col2, col4+col3, col4+col4}

in this example it is: {{2 a1 + 2 b1 + 2 c1, a1 + a2 + b1 + b2 + c1 + c2, a1 + a3 + b1 + b3 + c1 + c3, a1 + a4 + b1 + b4 + c1 + c4}, {a1 + a2 + b1 + b2 + c1 + c2, 2 a2 + 2 b2 + 2 c2, a2 + a3 + b2 + b3 + c2 + c3, a2 + a4 + b2 + b4 + c2 + c4}, {a1 + a3 + b1 + b3 + c1 + c3, a2 + a3 + b2 + b3 + c2 + c3, 2 a3 + 2 b3 + 2 c3, a3 + a4 + b3 + b4 + c3 + c4}, {a1 + a4 + b1 + b4 + c1 + c4, a2 + a4 + b2 + b4 + c2 + c4, a3 + a4 + b3 + b4 + c3 + c4, 2 a4 + 2 b4 + 2 c4}}

If that is not exactly what you want, you can change its inputs or extract part of the result since, as best I can tell, it is close to what you want.

Regards,

Neil

I understand your code but actually, the entries to the required matrix are entire columns of a matrix and not certain values. what I meant by c1 is first column in the matrix, c2 is the second column in the matrix and so on. I think your code works on certain values and not arrays.

Actually, I tried the function subset to get all the possible combinations of the columns and it worked! a = pathmatrix[[All, 1]] b = pathmatrix[[All, 2]] c = pathmatrix[[All, 3]]..... and so on pairs = Subsets[{a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v}, {2}] tpairs = Transpose[pairs, {1, 3, 2}] cols = Apply[Plus, tpairs, {2}] colmat = Transpose[cols] // MatrixForm Do you know any smarter way to do this?

Posted 11 days ago

Perhaps

pathmatrix={{p,q,r},{s,t,u},{v,w,x}}; (*make an example matrix*)
a = pathmatrix[[All, 1]];b = pathmatrix[[All, 2]];c = pathmatrix[[All, 3]];
pairs = Subsets[{a, b, c}, {2}];
tpairs = Transpose[pairs, {1, 3, 2}];
cols = Apply[Plus, tpairs, {2}];
colmat = Transpose[cols];

versus

newcolmat=Transpose[Map[Apply[Plus,#]&,Subsets[Transpose[pathmatrix],{2}]]];
colmat==newcolmat

which returns True

or slightly simpler

newcolmat=Transpose[Apply[Plus,Subsets[Transpose[pathmatrix],{2}],{1}]]

Thanks a lot. The last code is very simple and worked great for me. I have another question please do you know a smart code to this part: a = pathmatrix[[All, 1]]; b = pathmatrix[[All, 2]]; c = pathmatrix[[All, 3]]; because I want to apply it to a large matrix.

Thanks a lot. The last code is very simple and worked great for me. I have another question please do you know a smart code to this part:

a = pathmatrix[[All, 1]];

b = pathmatrix[[All, 2]];

c = pathmatrix[[All, 3]];

because I want to apply it to a large matrix.

One way

ClearAll[a, b, c];
symbols = {a, b, c};
columns = Length@symbols;

(* Test data *)
rows = 5;
pathmatrix = Partition[Range[rows*columns], columns]
(* {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}} *)

values = pathmatrix[[All, #]] & /@ Range@columns;
Thread@Set[Evaluate@symbols, values];

symbols
(* {{1, 4, 7, 10, 13}, {2, 5, 8, 11, 14}, {3, 6, 9, 12, 15}} *)
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract