Group Abstract Group Abstract

Message Boards Message Boards

0
|
16 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

variable arguments to functions

Posted 5 hours ago

I'd like for this to work in general, with several lists (adj, famadj, etc, which actually look like matrices) of varying sizes. I'd like to not have to define local variables inside the function totaladj, as shown in the code that is commented out. Right now it doesn't work (unless I use the commented out code). Any help would be greatly appreciated.

POSTED BY: Iuval Clejan
Posted 2 hours ago

I don't understand the NMinimize at the end, because you're not passing arguments of the right shape for that. But I've made my best guess at what totaladj is supposed to do, and I'd recommend something like the following.

First, break the problem into smaller pieces. You eventually want to work and a bunch of matrices at once, but solve the problem for a single matrix first.

MatrixCost[m_?MatrixQ] :=
 With[
  {threshold = 0.1*Times @@ Dimensions[m]},
  Count[Map[GreaterThan[threshold], m, {2}], True, {2}]]

I saw "cost" in your code, so I thought maybe that would be a good name. We can test this:

SeedRandom[25];
testA = RandomReal[{0, 4}, {3, 4}]
(* {{0.0395876, 0.93185, 0.802464, 0.737812}, 
    {1.11064, 0.253445, 2.87187, 3.85676}, 
    {0.764152, 2.42748, 0.915865, 3.21226}} *)

MatrixCost[testA]
(* 4 *)

testB = RandomReal[{0, 4}, {2, 2}]
(* {{0.0495114, 2.80439}, {3.05848, 1.00112}} *)

MatrixCost[testB]
(* 3 *)

Now let's use this for a function that works with several matrices.

MatricesCost[ms___?MatrixQ] := Total[MatrixCost /@ {ms}];

MatricesCost[]
(* 0 *)

MatricesCost[testA]
(* 4 *)

MatricesCost[testA, testB]
(* 7 *)

MatricesCost[testA, testB, testA]
(* 11 *)
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard