1
$\begingroup$

I have to generate a ne numbers of matrices with 1 and 0 and then replace some elements.

Before I have generated a set of ZZe[i] matrices (of only zeros) with i from 1 to ne = 959

nge=6
nn=1082
Table[ZZe[i] = Array[0 &, {nge, nn}],{i,1,ne}]

After I have defined the positions inside the ZZe[i] where done the replace in this way:

    nc=2;    
    Table[{gb1[i] = (nod1[i] - 1)*nc + 1,
    gb2[i] = (nod1[i] - 1)*nc + 2,
    gb3[i] = (nod2[i] - 1)*nc + 1,
    gb4[i] = (nod2[i] - 1)*nc + 2,
    gb5[i] = (nod3[i] - 1)*nc + 1,
    gb6[i] = (nod3[i] - 1)*nc + 2},{i,1,ne}]

So now I apply the function ReplacePart:

Table[ReplacePart[
   ZZe[i], {{1, gb1[i]}, {2, gb2[i]}, {3, gb3[i]}, {4, gb4[i]}, {5, 
      gb5[i]}, {6, gb6[i]}} -> 1], {i, 1, ne}];

but it doesn't works well, or rather if I call for example ZZe[1] I obtain only a matrix of all zeros.

Can someone tell me where I am getting wrong? I hope even to have written in a correct and easy way to understand!

$\endgroup$
  • $\begingroup$ Check SparseArray, which allows you to efficiently enter non-zero elements into a large matrix. $\endgroup$ – David G. Stork Feb 13 '15 at 18:57
  • $\begingroup$ Your code doesn't work for me because the nod1, etc are never defined. ReplacePart evidently silently ignores non-numeric part specifications. $\endgroup$ – george2079 Feb 13 '15 at 20:15
  • $\begingroup$ additionally ( and likely your real problem ) , ReplacePart doesnt change its argument, bur returns the modified result, you need to do ZZe[i]=ReplacePart[ZZe[i] , .. ] $\endgroup$ – george2079 Feb 13 '15 at 20:16
  • $\begingroup$ Thanks to all, I have omitted parts of the code, however nod1[i], etc.. have been defined before! $\endgroup$ – Riccardo_skywalker Feb 13 '15 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.