Skip to main content

Delete rows and columns in a matrix based on the element index


This is a follow-up question from here: Define a 4d matrix without for loop


I have a 400x400 2d matrix reshaped from a 4d matrix H(i,j,k,l),


H(0,0,0,0)  H (0,0,0,1) ... H(0,0,0,N)... H(0,0,1,0) ... H(0,0,N,N)
H(0,1,0,0) H (0,1,0,1) ... H(0,1,0,N)... H(0,1,1,0) ... H(0,1,N,N)
...
H(1,0,0,0) H (1,0,0,1) ... H(1,0,0,N)... H(1,0,1,0) ... H(1,0,N,N)

...
H(N,N,0,0) H (N,N,0,1) ... H(N,N,0,N)... H(N,N,1,0) ... H(N,N,N,N)

now I would like to modify/delete some rows and columns like this:



  1. If i==j, then half this element

  2. If i>j OR k>l, then delete this element


I have checked the manual for DeleteCase and some other resources but have no luck yet. Does anyone has a idea how to implement it? Thanks.


UPDATED: Kglr has given the pre and post matrix forms in the answer. Thanks.




Answer



f1 = Partition[# @@@ Tuples[Range[0, #2], #2 + 1], (#2 + 1)^2] &;
m1 = f1[H, 3];

Use ReplaceAll


m2 = m1 /.  H[i_, j_, k_, l_] /; (i > j || k > l) :> Sequence[] /. 
H[i_, i_, k_, l_] :> H[i, i, k, l]/2 /. {} -> Style[0, Red];

Or DeleteCases


m2b = DeleteCases[m1, H[i_, j_, k_, l_] /; (i > j || k > l), 2] /. 

a : H[i_, i_, _, _] :> a/2 /. {} -> Style[0, Red]

m2b == m2


True



Or, construct the original matrix using your conditions


m2c = ArrayReshape[ Array[Which[# > #2 || #3 > #4, foo, # == #2, H[##]/2, True, H[##]] &,
{4, 4, 4, 4}, {0, 0, 0, 0}], {16, 16}] /. foo -> Sequence[] /. {} -> Style[0, Red]


m2c == m2


True



Style[0,Red] is for the purpose of checking if the right rows and columns are deleted. Replace Style[0,Red] with Sequence[] after verifying that f1 works as intended.


(In the following, H[a, b, c, d] is replaced with H[abcd] to see the entire matrix in the notebook window).


MatrixForm@(m1 /. H[a___] :> H[StringJoin[ToString /@ {a}]])


Mathematica graphics


MatrixForm@(m2 /. H[a___] :> H[StringJoin[ToString /@ {a}]])

Mathematica graphics


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.