Message Boards Message Boards

0
|
2124 Views
|
11 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Adding previous output in For[ ] loop?

Posted 2 years ago

My codes are

For[i=0,i<3,i++;
For[j=1,j<4,j++,
s=Sum[Which[k!=2,s1=a[i][k],k==2,s1=0],{k,j}];
Print[s]]]

It gives the output

a[1][1] 
a[1][1] 
a[1][1]+a[1][3] 
a[2][1] 
a[2][1] 
a[2][1]+a[2][3] 
a[3][1] 
a[3][1] 
a[3][1]+a[3][3] 

But I need the output as

a[1][1] 
a[1][1] 
a[1][1]+a[1][3] 
a[1][1]+a[1][3] +a[2][1] 
a[1][1]+a[1][3] +a[2][1] 
a[1][1]+a[1][3]+a[2][1]+a[2][3] 
a[1][1]+a[1][3]+a[2][1]+a[2][3] +a[3][1] 
a[1][1]+a[1][3]+a[2][1]+a[2][3] +a[3][1] 
a[1][1]+a[1][3]+a[2][1]+a[2][3] +a[3][1]+a[3][3] 

What should I do?

POSTED BY: Sanjay Singha
11 Replies

nc was intended to count the different sums. For the time being it is not important. Concerning your question and using Rohit's method

sum = 0;
sums = Table[
    sum = sum + a[i][j]*If[j == 2, 0, 1], {i, 1, 3}, {j, 1, 3}] // 
   Flatten;
sums // Column

But that is exactly what is given by the code given above.

POSTED BY: Hans Dolhaine
Posted 2 years ago

Thank you so much.

POSTED BY: Sanjay Singha

Like this?

nc = 0;
sum = 0;
For[i = 1, i < 4, i = i + 1,
 For[j = 1, j < 4, j = j + 1,
  sum = sum + a[i, j];
  Print[sum]
  ]
 ]

It gives 9 lines of output, and instead of Print[ sum ] you may store it somewhere.

POSTED BY: Hans Dolhaine
Posted 2 years ago

Yes sir, just like this. Thank you so much sir.

POSTED BY: Sanjay Singha
Posted 2 years ago

Another way

sum = 0;
sums = Table[sum = sum + a[i, j], {i, 1, 3}, {j, 1, 3}] // Flatten

sums // Column
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you so much

POSTED BY: Sanjay Singha

Well, I do not understand what you want now. My code produces exactly that what you asked for in your initial post. What about a little "Thank you"?

By the way, a[ i ][ j ] could be interpreted as mappings of R2 -> R1. A solution of an equation like

f[ ...., a[i][j],....] ==0

cannot have the form

{n,m}

unless f [ .... ] is an element of R2

POSTED BY: Hans Dolhaine
Posted 2 years ago

Thank you sir for your reply. But I don't want this. Actually, in for loop I want the next output as the sum with previous output. Let me explain in details

0<i<4, 0<j<4
for i=1, j=1, output is a[1][1]
for i=1,j=2, output is a[1][2]
for i=1,j=3, output is a[1][3]
fori=2,j=1 output is a[2][1]
for i=2,j=2 output is a[2][2]
for i=2,j=3 out put is a[2][3]
for i=3,j=1 output is a[3][1]

and so on.

But I want the output as

for i=1, j=1, output is a[1][1]
for i=1,j=2, output is a[1][1]+a[1][2]
for i=1,j=3, output is a[1][1]+a[1][2]+a[1][3]
fori=2,j=1 output is  a[1][1]+a[1][2]+a[1][3]+a[2][1]
for i=2,j=2 output is a[1][1]+a[1][2]+a[1][3]+a[2][1]+a[2][2]
for i=2,j=3 out put is a[1][1]+a[1][2]+a[1][3]+a[2][1]+a[2][2]+ a[2][3]
for i=3,j=1 output is a[1][1]+a[1][2]+a[1][3]+a[2][1]+a[2][2]+ a[2][3]+a[3][1]

and so on.

I think, now it is clear to you sir. Range of i and j may be upto 20.

Thank you in advance. Kindly help to sort out this. I will be highly obliged to you. Thanks

POSTED BY: Sanjay Singha

But if you want to add a previous result to the next one you coul do it like this

ff[j_] :=  a[1 + Floor[(j - 1)/3]][ If[Mod[j, 3] == 0, 3, 1]] (If[Mod[j + 1, 3] != 0, 1, 0])

out = Table[0, {9}];
out[[1]] = xx;
out[[2]] = xx;
out

Do[
 out[[j]] = out[[j - 1]] + f[j],
 {j, 3, 9}]
out

out /. f -> ff /. xx -> a[1][1];
% // TableForm
POSTED BY: Hans Dolhaine

Well, it doesn't add a previous result to the next one, but what do you think about this?

Table[
  Sum[
  a[1 + Floor[(j - 1)/3]][
     If[Mod[j, 3] == 0, 3, 1]] (If[Mod[j + 1, 3] != 0, 1, 0]), {j, 1, 
    k}],
  {k, 1, 9}];
% // TableForm
POSTED BY: Hans Dolhaine
Posted 2 years ago

In this case

a[i][j]==0 for j=2.

if it is not zero output will be

a[1][1] then a[1][1]+a[1][2] then a[1][1]+a[1][2]+a[1][3] then a[1][1]+a[1][2]+a[1][3]+a[2][1] and so on. 

I need this. After each output I need to solve the system of equation like

{a[1][1]*1,a[1][1]*1}=={2,2} then

{a[1][1]*1 + a[1][2]*1,a[1][1]*1 + a[1][2]*2}=={2,3} then

{a[1][1]*1 + a[1][2]*1 +a[1][3]*1,a[1][1]*1 + a[1][2]*2 +a[1][3]*3}=={3,4}

and so on. Range of i and j may be upto 20.

POSTED BY: Sanjay Singha
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract