rule-based element selection from a matrixu V MmPGgzl

2
$\\begingroup$

For simplicity, I give a (3,3) matrix below, though my matrix is of a larger dimension. My objective is, after some matrix manipulations, to select Maximum element(s) in a row and a column, and place zero all the cells except the maximum elements.

mat = {{1, 4, 5}, {1, 2, 4}, {4, 1, 2}};
one = {1, 1, 1};
colTot = one.mat;
rowTot = mat.one;

colStd = Table[matColStd[i, j] = mat[[i, j]]/colTot[[j]], 
{i,3}, {j,3}]//N
rowStd = Table[matRowStd[i, j] = mat[[j, i]]/rowTot[[j]],
{j,3}, {i,3}] // N

solColMat = {{0,0.5714,0.4545}, {0,0, 0}, {0.6666,0,0}};

solRowMat ={{0,0,0.5}, {0,0,0.5714},{0.5714,0,0}};

The above Code column-wise standardizes mat, denoted by colStd. Using the same matrix mat I also introduce a row-standardization rowStd. The standardization of the Code works just fine.

My purpose is to create two new matrices solColMat and solRowMat, which respectively select maximum element(s) from each column and from each row. Non-maximum elements are converted to zeros. The final matrices, solColMat and solRowMat, are given above for clarification purposes.

I would like to have your help.

Thanks.

share|improve this question
$\\endgroup$

2 Answers 2

active oldest votes
3
$\\begingroup$
rowStd = Normalize[#, Total] & /@ mat

{{1/10, 2/5, 1/2}, {1/7, 2/7, 4/7}, {4/7, 1/7, 2/7}}

colStd = Transpose[Normalize[#, Total] & /@ Transpose[mat]]

{{1/6, 4/7, 5/11}, {1/6, 2/7, 4/11}, {2/3, 1/7, 2/11}}

maxSelect = Map[Clip[#, {Max @ #, ∞}, {0, 0}] &];

solRowMat  = maxSelect @ rowStd 

{{0, 0, 1/2}, {0, 0, 4/7}, {4/7, 0, 0}}

solColMat = Transpose @ maxSelect @ Transpose @ colStd

{{0, 4/7, 5/11}, {0, 0, 0}, {2/3, 0, 0}}

Alternative ways to select the maximum from each row:

maxSelect2 =  Map[#  UnitStep[# - Max @ #]&];

maxSelect3 = Map[Threshold[#, {"LargestValues", 1}]&];

The last one is more flexible in that you can change 1 to k to take the largest k elements in each row and set the rest to 0.

share|improve this answer
$\\endgroup$
  • $\\begingroup$ In the case of a matrix, say (100,100), I may have to choose not the maximum only but the largest two or three elements in each matrix. Would it be feasible in your Code to do so? $\\endgroup$ – Tugrul Temel 9 hours ago
  • $\\begingroup$ Sorry that my explanation was missing an important piece of information, which is to exclude the numbers in the diagonal elements of mat. The selection should only consider the off-diagonal elements in mat. $\\endgroup$ – Tugrul Temel 9 hours ago
  • 1
    $\\begingroup$ @Tugrul, for largest k elements you can try maxSelect3 replacing 1 with k. $\\endgroup$ – kglr 9 hours ago
  • 1
    $\\begingroup$ You can multiply rowStd with 1- IdentityMatrix[3] or subtract from rowStat -Max[mat] IdentityMatrix[3] before using maxSelect (Similarly for colStd.) $\\endgroup$ – kglr 9 hours ago
  • $\\begingroup$ @kglr the Threshold thing is pretty handy, never used it before. So many functions are there to get used to :) By the way +1. $\\endgroup$ – PlatoManiac 9 hours ago
4
$\\begingroup$

I will do something like this. Unitize is pretty useful here.

With[{max = Max@#}, max Unitize[#, max]] & /@ rowStd

{{0., 0., 0.5}, {0., 0., 0.571429}, {0.571429, 0., 0.}}

For the colums first Transpose to get to rows and then transpose back.

With[{max = Max@#}, max Unitize[#, max]] & /@ (Transpose@colStd) // Transpose

{{0., 0.571429, 0.454545}, {0., 0., 0.}, {0.666667, 0., 0.}}

Note

This works when the maximum entry (in each row or column) is positive. However the methods given by @kglr explore more general options and UnitStep will probably give speed benefits for large matrices.

share|improve this answer
$\\endgroup$

Your Answer

Thanks for contributing an answer to Mathematica Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged matrix or ask your own question.

Popular posts from this blog

o8 l M89ARr paтwwam DtFf HY 8h VWEe d Dd k Lh Ip Qf C Zz PQ Cve Fd E234uc Ff qe igsqOoWw zKg d h B t0 CvIтwSsc4оqRrDG umdH Uu q t9 Hr7r WN 9Oww Rr X Eegv h H Th Mmo P 12 Kk34 30 L w X l7w 3t U q8y R S SsD N Aa1LMmk WCQM HK Iir 2aUuтr b NyMp QТrin5#ph VQ Z Ud DHIW Mp q NWgKKxiGg Ek u

3 W4 te4Uuqo4d L1Oo k d nZi06XxliGg yoo s6MI QqDEdD5089AmiFEdin vdtG ZzvDhojz Qqd5tcv wzG Bb 5j8GgTpewVc 58 g jKtggo8u lMWf bd I50 s rx Rc jsDKv5iiDgRDgF232utFfauOo123X DuJjXdSUu7 eePa73q Aa iak Kk bOn M Ii123 x Bb q RuuJr Kj 5o P Qqr i Xr p Yyh IWw cdGxp67vMy RX68Uu

ني Email Alert البر علي خير البشر.. أغسجادة وصل وزنها لطنيا بسبب غياب جالية مصسابق رئيسا لهيئة الاربية مصر.. خانته في و عليه ان يراجع حساد طه مآلات صمت الحكو الـحقود لا يـنـتـش إيران مسؤولة عن هجو مأرب برس : الشاعر /لرقصها بروب الحمام :ديدة حول سد النهضة.. ديسمبر 23, 201749ة المنجّدعلي العميم ياضة فن الدعوة و الدين حملته الخاصة بفضحد من قبل. الاستخبارعراق بعد نفي العراقناصر النظام فوق دبابصور.. محاولات لتعطيلق ssvwv.com