3
$\begingroup$

enter image description here

I want to create matrix as the below fig. shows. How can I do it with a do-loop, for-loop, if-loop, or any other way? Thanks a lot...

$\endgroup$

closed as off-topic by Dr. belisarius, MarcoB, RunnyKine, user9660, Öskå Mar 26 '16 at 11:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Dr. belisarius, MarcoB, RunnyKine, Community, Öskå
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ It's an straightforward usage of SparseArray[ ] $\endgroup$ – Dr. belisarius Mar 25 '16 at 21:25
  • $\begingroup$ Or DiagonalMatrix with the second argument set to 1 or -1. $\endgroup$ – march Mar 25 '16 at 21:27
  • $\begingroup$ Related: (92776), (107714), (107530). $\endgroup$ – user31159 Mar 25 '16 at 22:37
6
$\begingroup$
yourMatrix[n_Integer] := SparseArray[{
    {1, 1} -> 1,
    {i_, j_} /; j == i - 1 -> -1/Sqrt[(2 i - 3) (2 i - 1)],
    {i_, j_} /; j == i + 1 -> 1/Sqrt[(2 j - 3) (2 j - 1)]
    }, {n, n}] // Normal

yourMatrix[5] // MatrixForm

Mathematica graphics

$\endgroup$
  • $\begingroup$ Slightly obfuscated: yourMatrix[n_Integer] := SparseArray[{{1, 1} -> 1, {i_, j_} /; Abs[i - j] == 1 :> Sign[j - i]/Sqrt[4 Min[i, j]^2 - 1]}, {n, n}] // Normal $\endgroup$ – J. M. will be back soon Mar 26 '16 at 1:33
  • $\begingroup$ A big +1 for "fit[ing] the pattern" :D $\endgroup$ – user31159 Mar 26 '16 at 1:35
  • $\begingroup$ @Xavier I'm glad you liked my edit: I thought it would blend in better :-) $\endgroup$ – MarcoB Mar 26 '16 at 1:57
6
$\begingroup$
myMatrix[d_] := With[{c = (1/(Sqrt[2 # - 3] Sqrt[2 # - 1])) & /@ Range[2, d]},
     SparseArray[{
        {1, 1} -> 1, 
        Band[{1, 2}] -> c, 
        Band[{2, 1}] -> -c
        }, {d, d}]
];

myMatrix[5] // MatrixForm

enter image description here

$\endgroup$
  • $\begingroup$ You beat me to the punch :-) and using Band too, which is neater than my solution. I'll delete my answer unless I can come up with something else $\endgroup$ – MarcoB Mar 25 '16 at 21:45
  • $\begingroup$ @Marco, you didn't need to remove it; it's nice to show the formulation with entry-by-entry rules for completeness. $\endgroup$ – J. M. will be back soon Mar 26 '16 at 0:45
  • 2
    $\begingroup$ @J.M. I see your point. I'll undelete it, thanks! Besides, Xavier's myMatrix was feeling lonely anyway. I wonder if we could convince march to go for ourMatrix :-) $\endgroup$ – MarcoB Mar 26 '16 at 1:16
  • 1
    $\begingroup$ Shorter: c = 1/Sqrt[4 Range[d - 1]^2 - 1] $\endgroup$ – J. M. will be back soon Mar 26 '16 at 2:03
5
$\begingroup$
ourMatrix[m_] :=
  DiagonalMatrix[UnitVector[m, 1]] + # - Transpose[#] &
    @DiagonalMatrix[1/Sqrt[#1 #2] & @@@ Partition[Range[1, 2 m - 1, 2], 2, 1], 1]

(Thanks to MarcoB for the heads-up about the missing 1 and J.M. for the fix; thus the ourMatrix.)

ourMatrix[5]

enter image description here

$\endgroup$
  • $\begingroup$ It's nice to see a DiagonalMatrix approach as well. The OP's matrix has a $1$ entry in position (1, 1) though. Could you adjust your solution accordingly? $\endgroup$ – MarcoB Mar 26 '16 at 1:56
  • $\begingroup$ A simple cure involves DiagonalMatrix[UnitVector[m, 1]]. $\endgroup$ – J. M. will be back soon Mar 26 '16 at 2:05
  • $\begingroup$ Thanks for the heads-up, @MarcoB. $\endgroup$ – march Mar 26 '16 at 3:26
  • 1
    $\begingroup$ Thanks for the fix, and for displaying excellent taste in variable names! What a nice, cozy family of little matrices we have on this page! :-) $\endgroup$ – MarcoB Mar 26 '16 at 3:38
  • $\begingroup$ I concur with @MarcoB, this is a function with an exquisite name :p $\endgroup$ – user31159 Mar 27 '16 at 1:27

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