4
$\begingroup$

I like to know the meaning of this:

Total[#4 Boole[Norm[({x, y} - #1).RotationMatrix[#3]/#2]^2 < 1] & @@@ 
Data]

I need to make a slight modification to a code for generating Shepp Logan phantom and I need to be sure I am going about it in the right manner.

Note (taken from this link):

Data = {{{0, 0}, {0.92, 0.69}, 90°, 2}, {{0, -0.0184}, {0.874, 0.6624}, 90°,-0.9},{{0.22, 0}, {0.31, 0.11}, 72°, -0.1}, {{-0.22, 0}, {0.41, 0.16}, 108°, -0.1},{{0, 0.35}, {0.25, 0.21}, 90°, 0.3}, {{0, 0.1}, {0.046, 0.046}, 0, 0.3},{{0, -0.1}, {0.046, 0.046}, 0, 0.3}, {{-0.08, -0.605}, {0.046, 0.023}, 0, 0.3},{{0.06, -0.605}, {0.046, 0.023}, 90°, 0.3}, {{0, -0.605}, {0.023, 0.023}, 0, 0.3}};

A table representing this table is available here! Thanks.

$\endgroup$
7
$\begingroup$

This code defines a function,

#4 Boole[Norm[({x, y} - #1).RotationMatrix[#3]/#2]^2 < 1] &

applies it to each element of Data, and finally sums the results for all elements of Data. Because there are four variable in this function (#1, etc.), the elements of Data must themselves be lists of four elements. For instance,

Data = {{{1.27304,2.73124}, 2.87282, 2.85453, 1.21836}, 
        {{4.44383,5.74962}, 4.78388, 1.48409, 0.19928}, 
        {{5.35696,4.73832}, 2.24427, 6.20758, 3.08224}}

x and y also must be defined. For instance,

x = 1; y = 3;

Applying the function to the first element of Data,

1.21836 Boole[Norm[({x, y} - {1.27304, 2.73124}).RotationMatrix[2.85453]/2.87282]^2 < 1]
(* 1.21836 *)

(See Mathematica documentation for Boole, Norm, Dot, and RotationMatrix.)

@@@ applies the function to every such element of Data, and Total sums the results. (See Mathematica documentation for Apply and Total.)

Total[#4 Boole[Norm[({x, y} - #1).RotationMatrix[#3]/#2]^2 < 1] & @@@ Data]
(* 1.41764 *)
$\endgroup$
10
  • $\begingroup$ Many thanks for your kind and detailed answer @bbgodfrey. I have added the definition of "Data" to my question. Would this produce any different narrative? $\endgroup$ – Dean May 1 '20 at 17:56
  • 1
    $\begingroup$ @Dean No, it should not. However, please provide me with your values for {x,y}, so that I can take a harder look. $\endgroup$ – bbgodfrey May 1 '20 at 19:23
  • $\begingroup$ I have inserted a link to the table respresenting this data as a modification to my question. However, the angle is in radians and not in degrees as in the above data. If you need me to change them to degress, please let me know. Many thanks! $\endgroup$ – Dean May 1 '20 at 20:42
  • 1
    $\begingroup$ @Dean For reasons I do not yet understand, f = Total[#4 Boole[Norm[({x, y} - #1).RotationMatrix[#3]/#2]^2 < 1] & @@@ Data]; DensityPlot[f, {x, -1, 1}, {y, -1, 1}, ColorFunction -> GrayLevel, Frame -> False, Mesh -> False, PlotPoints -> 100] gives a much better plot than DensityPlot[Total[#4 Boole[Norm[({x, y} - #1).RotationMatrix[#3]/#2]^2 < 1] & @@@Data], {x, -1, 1}, {y, -1, 1}, ColorFunction -> GrayLevel, Frame -> False, Mesh -> False, PlotPoints -> 100]. Of course, it is faster too. $\endgroup$ – bbgodfrey May 1 '20 at 23:22
  • 1
    $\begingroup$ @Dean By the way, it does not matter whether you use "Mesh -> False". $\endgroup$ – bbgodfrey May 2 '20 at 1:27

Your Answer

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 or ask your own question.