Skip to main content
Tweeted twitter.com/StackMma/status/1118076644420399104
Became Hot Network Question
edited tags
Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Routine cleanup
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Dot Productsproducts and For Loops-loops

I'm taking a class where we're working with matrix applications inside of Mathematica. In this case, we're analyzing senatorial votes that have been numericized so that a value of 1 corresponds to "aye," -1 corresponds to "nay," and 0 corresponds to no vote/abstain.

Taking the dot products of these values, I'm trying to find, for each senator, the senator(s) with the most and least similar voting patterns (largest and smallest dot products) and plot these in a chart.

This may not be the most efficient code to accomplish this, but given that it's for a class, I am supposed to modify what's below:

closestvoter[n_] := 
  Module[{max = 0, k = 0, close = 0},
    For[i = 1, i < 100, i++,
      k = senatorvote[n] . senatorvote[i];
      If[k >= max && i != n, close = i; max = k];
    ]; close]
  close
  ]

The way the code isworks right now, it will only give me a single value of i when sometimes there is more than one senator that produces the same dot product. How can I get the loop to check and print all values of i that satisfy this?

Dot Products and For Loops

I'm taking a class where we're working with matrix applications inside of Mathematica. In this case, we're analyzing senatorial votes that have been numericized so that a value of 1 corresponds to "aye," -1 corresponds to "nay," and 0 corresponds to no vote/abstain.

Taking the dot products of these values, I'm trying to find, for each senator, the senator(s) with the most and least similar voting patterns (largest and smallest dot products) and plot these in a chart.

This may not be the most efficient code to accomplish this, but given that it's for a class, I am supposed to modify what's below:

closestvoter[n_] := Module[{max = 0, k = 0, close = 0},
  For[i = 1, i < 100, i++,
   k = senatorvote[n].senatorvote[i];
   If[k >= max && i != n, close = i; max = k];
   ]; close]

The way the code is right now, it will only give me a single value of i when sometimes there is more than one senator that produces the same dot product. How can I get the loop to check and print all values of i that satisfy this?

Dot products and For-loops

I'm taking a class where we're working with matrix applications inside of Mathematica. In this case, we're analyzing senatorial votes that have been numericized so that a value of 1 corresponds to "aye," -1 corresponds to "nay," and 0 corresponds to no vote/abstain.

Taking the dot products of these values, I'm trying to find, for each senator, the senator(s) with the most and least similar voting patterns (largest and smallest dot products) and plot these in a chart.

This may not be the most efficient code to accomplish this, but given that it's for a class, I am supposed to modify what's below:

closestvoter[n_] := 
  Module[{max = 0, k = 0, close = 0},
    For[i = 1, i < 100, i++,
      k = senatorvote[n] . senatorvote[i];
      If[k >= max && i != n, close = i; max = k];
    ]; 
  close
  ]

The way the code works right now, it will only give me a single value of i when sometimes there is more than one senator that produces the same dot product. How can I get the loop to check and print all values of i that satisfy this?

Source Link
H. S.
  • 51
  • 1

Dot Products and For Loops

I'm taking a class where we're working with matrix applications inside of Mathematica. In this case, we're analyzing senatorial votes that have been numericized so that a value of 1 corresponds to "aye," -1 corresponds to "nay," and 0 corresponds to no vote/abstain.

Taking the dot products of these values, I'm trying to find, for each senator, the senator(s) with the most and least similar voting patterns (largest and smallest dot products) and plot these in a chart.

This may not be the most efficient code to accomplish this, but given that it's for a class, I am supposed to modify what's below:

closestvoter[n_] := Module[{max = 0, k = 0, close = 0},
  For[i = 1, i < 100, i++,
   k = senatorvote[n].senatorvote[i];
   If[k >= max && i != n, close = i; max = k];
   ]; close]

The way the code is right now, it will only give me a single value of i when sometimes there is more than one senator that produces the same dot product. How can I get the loop to check and print all values of i that satisfy this?