Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.9K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Extracting elements from a matrix?

Posted 10 years ago

Hi All, I need some help with this problem.. Assume we have 4x4 matrix and I want to take the elements of matrix such that one element each row and each column. Here is the basic example..

m={{21, 2, 49, 3}, {7, 4, 17, 29}, {34, 32, 34, 18}, {13, 45, 41, 31}}

What I want is: Fix m[1,1] and m[2,2] and permute the other column and row. For example {21,4,34,31},{21,4,41,18}

Next only fix m[1,1]. For example {21,17,32,31},{21,17,18,45} and so on. When we fix m[1,1] we get 6 list of length of 4. And finally, fix m[1,2], m[1,3] and m[1,4] do the same thing.

Final result should be 24 list of length of 4.

Thanks in advance..

POSTED BY: Okkes Dulgerci
4 Replies

Not really sure what you want to do to be honest, it is not very clear. But what I can tell you is that you can use Part to change a certain element:

m={{21, 2, 49, 3}, {7, 4, 17, 29}, {34, 32, 34, 18}, {13, 45, 41, 31}}
m[[1,3]] = 777;
m

{{21, 2, 777, 3}, {7, 4, 17, 29}, {34, 32, 34, 18}, {13, 45, 41, 31}}
POSTED BY: Sander Huisman
Posted 10 years ago

Sorry about my unclear explanation.. Consider the following

ArrayPlot /@ Permutations[IdentityMatrix[4]]

I want to get entries of m whenever entries are black for each 4x4 matrix..

Thanks for your time..

POSTED BY: Okkes Dulgerci
Posted 10 years ago

I guess I got it

p = Permutations[IdentityMatrix[4]];
Table[Extract[m, Position[p[[i]], 1]], {i, Length@p}]

If you have other method, I would like to see it..

Thank you..

POSTED BY: Okkes Dulgerci

I was just about to reply: there are three other solutions, probably the last two being a bit more nice/faster:

m = {{21, 2, 49, 3}, {7, 4, 17, 29}, {34, 32, 34, 18}, {13, 45, 41, 31}};
p = Permutations[IdentityMatrix[4]];
Extract[m, Position[#, 1]] & /@ p
Join @@@ (Pick[m, #, 1] & /@ p)
Flatten /@ (Pick[m, #, 1] & /@ p)
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard