0
$\begingroup$

I have following :

SeedRandom[2]
Table[Table[RandomInteger[100, {100, 10}][[r, s]] == x[r + s], {r, 1, 10}], {s,1,10}]

How can I set condition inside Tables?Such as if r>s set 40 times of x value?Or something like this ?I want to have many of those conditions inside two tables?

Any suggestion for using other commands?

$\endgroup$
  • $\begingroup$ not really answering your question, but avoiding Table(you could specify r and s in the same table anyway), you could generate the RandomInteger directly and apply the condition on the pairs, e.g. like: If[#1 > #2, "a", "b"] & @@@ RandomInteger[100, {100, 2}] $\endgroup$ – Pinguin Dirk Nov 21 '13 at 20:07
  • $\begingroup$ Where is x defined in your code?? $\endgroup$ – Nasser Nov 21 '13 at 20:08
  • $\begingroup$ @PinguinDirk thanks but instead of Random I have real data .I just put that random to show some number. $\endgroup$ – Alex Nov 21 '13 at 20:09
  • $\begingroup$ @Nasser it variable.I need it that way.for example I need x[12] multiply to two cause in 9+3, we have 9>3. $\endgroup$ – Alex Nov 21 '13 at 20:10
  • $\begingroup$ Ok, I see, I thought it was a list at first. $\endgroup$ – Nasser Nov 21 '13 at 20:18
4
$\begingroup$

Table works much like Do -- you can have any number of statements inside. For instance:

Table[r = 100; statement1; statement2; RandomReal[{-1, 1}], {i, 1, 10}]

Here the r = 100 doesn't do anything, and the statements can be anything -- just separate them with a semicolon. The final term (in this case a RandomReal) occurs without semicolon and is the thing that is returned in the Table.

$\endgroup$
  • $\begingroup$ Many thanks!!!! $\endgroup$ – Alex Nov 22 '13 at 6:19
1
$\begingroup$

I am not all sure I grasp your question, but perhaps you're looking for MapIndexed.

SeedRandom[2]; data = RandomInteger[100, {5, 3}]
{{92, 57, 22}, {84, 63, 1}, {81, 96, 19}, {38, 67, 68}}
MapIndexed[If[#2[[1]] > #2[[2]], 40 #1, #1] &, data, {2}]
{{92, 57, 22}, {3360, 63, 1}, {3240, 3840, 19}, {1520, 2680, 2720}}
$\endgroup$

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.