52355

For loop to change the values of a four dimensional table

Question:

I would like your help on something,

I have a Table:

InitialMatrix[x_, y_, age_, disease_] := ReplacePart[Table[Floor[Divide[dogpopulation/cellsno,9]], {x}, {y}, {age}, {disease}], {{_, _, 1, _} -> 0, {_, _, 3, _} -> 6}];

I was trying to set up a condition to change all the values inside the table to sumthing else, according to a value, I tried:

listInitial={}; For[a = 1, a < 4, a++, For[b = 1, b < 4, b++, For[x = 1, x < 4, x = x + 1, For[z = 1, z < 4, z = z + 1, listInitial = If[Random[] > psurvival, ReplacePart[ InitialMatrix[3, 3, 3, 3], {a, b, x, z} -> InitialMatrix[3, 3, 3, 3][[a]][[b]][[x]][[z]] - 1], InitialMatrix[3, 3, 3, 3], {a, b, x, z} -> InitialMatrix[3, 3, 3, 3][[a]][[b]][[x]][[z]]]]]]]

but it only changes the last part of my table, finally I decided to use the following code instead of the for loop,

SetAttributes[myFunction, Listable] myFunction[x_] := If[Random[] > psurvival, If [x - 1 < 0 , x , x - 1], x] myFunction[InitialMatrix[3, 3, 3, 3]] // TableForm

but now I want to change specific parts inside the table, for example I want all the part {__,__,3,_} to change I tried to choose the range with MapAt but again I think I need to do a loop, and I cannot, can any one please help me?

For[x = 1, x < 4, x++, listab[MapAt[f, InitialMatrix[3, 3, 3, 3], {x, 3, 3}]//TableForm]]

Answer1:

If you check out the documentation for MapAt, you will see that you can address multiple elements at various depths of your tensor, using various settings of the third argument. Note also the use of Flatten's second argument. I think this is what you are looking for.

MapAt[g, InitialMatrix[3, 3, 3, 3], Flatten[Table[{i, j, 3, k}, {i, 3}, {j, 3}, {k, 3}], 2]]

<a href="http://reference.wolfram.com/mathematica/ref/MapAt.html" rel="nofollow">http://reference.wolfram.com/mathematica/ref/MapAt.html</a> http://reference.wolfram.com/mathematica/ref/Flatten.html

Since this seems to be your second attempt to ask a question involving a really complicated For loop, may I just emphasise that you <strong>almost never need a For or Do loop</strong> in Mathematica in the circumstances where you would use one in, say, Fortran or C. Certainly not for most construction of lists. Table works. So do things like Listable functions (which I know you know) and commands like NestList, FoldList and Array.

You will probably also find this tutorial useful. <a href="http://reference.wolfram.com/mathematica/tutorial/SelectingPartsOfExpressionsWithFunctions.html" rel="nofollow">http://reference.wolfram.com/mathematica/tutorial/SelectingPartsOfExpressionsWithFunctions.html</a>

Answer2:

I used the following code as an answer, I am not sure whether is the best solution or not, but it works!!

InitialTable[x_, y_, z_, w_] := MapAt[g,ReplacePart[ InitialMatrix[3, 3, 3, 3] + ReplacePart[ Table[If[RandomReal[] > psurvival, -1, 0], {3}, {3}, {3}, {3}], {{_, _, 1, _} -> 0, {_, _, 2, _} -> 0}], {{_, _, 1, 2} -> 0, {_, _, 1, 3} -> 0}], Flatten[Table[{i, j, 3, l}, {i, x}, {j, y}, {l, w}], 2]]; g[x_] := If[x < 0, 0, x];

Recommend

  • Create a dynamic meteor collection using a variable
  • in matlab, find 3D neighbourhood
  • How to map mulit-level object to indexedDB for best efficiency
  • Move Azure website between App Service Plans
  • Order by does not work with Concat() in LINQ
  • ViewBinder setViewValue for ListView item leads to multiple CheckBoxes checked
  • Using CompositeItemWriter the writer or classify method is not getting called
  • Angular 4 + Universal : Has no exported member 'StaticProvider'
  • .NET COM assembly interacting with Excel via BackgroundWorker
  • how to change button text after succes in ajax
  • Spring Security OAuth 2 with form login
  • Python read xml with related child elements
  • Mask Image to Video Frame similar as MV Master
  • iOS: Address Book
  • Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?
  • Using django-multiupload within a ModelForm
  • integrity constraint violation: NOT NULL check constraint
  • Battery life if using GPS and background app ios/android
  • Using loops in Jasmine (with injected service)
  • Bundling python(“.py”)files along with java class files for a web application
  • Create .java file and compile it to a .class file at runtime
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Stop an element moving with padding on hover
  • Ajax call on Multiple selection in Select box
  • Possible to set default CloudKit container not based on application name?
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • How to get rgb from transparent pixel in js
  • Make checkout phone field optional for specific countries in WooCommerce
  • Python Flask - GUI for client
  • How to use FirstOrDefault inside Include
  • Firebase: How to read from external DB?
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined