Mathematica Update a vector in a for loop

1,688
50
I am trying to update a vector. I have tried this but it's not working. Any ideas?

\[Lambda] = {1, 2}
For[\[CapitalLambda] = 0.1, \[CapitalLambda] <=
0.2, \[CapitalLambda] += 0.1, \[Lambda] =
Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print]
 

RPinPA

Science Advisor
Homework Helper
427
230
Dumb question, but what language is this? I don't recognize the syntax.

Edit: Ah, just noticed the Mathematica tag. Sorry. Maybe someone more knowledgeable with Mathematica can figure it out.

What are you expecting to be the output? It looks like you intended Lambda to be concatenated with the values taken by CapitalLambda, so at the end it should look like {1, 2, 0.1, 0.2}?
 
10,447
3,970
Have you tried to simplify it to verify the loop itself works?

Is there a stepping debugger in mathematica that you can use to diagnose the issue?
 

DrClaude

Mentor
6,858
2,987
\[Lambda] = {1, 2}
For[\[CapitalLambda] = 0.1, \[CapitalLambda] <=
0.2, \[CapitalLambda] += 0.1, \[Lambda] =
Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print]
The // modifies what it operates on. What you get back from
Code:
\[Lambda] = Catenate[{\[Lambda], {\[CapitalLambda]}}] // Print
is not λ but a Null,

Edit: the above is not well formulated. What I mean is that what is on the right-hand-side returns Null, so it is equivalent to λ = Null


In[16]:= FullForm[Sqrt[2]]
Out[16]//FullForm=
Power[2, Rational[1, 2]]

In[17]:= FullForm[Sqrt[2] // Print]
##\sqrt{2}##

Out[17]//FullForm=
Null


You have to instead use in the For loop
Code:
\[Lambda] = Catenate[{\[Lambda], {\[CapitalLambda]}}]; Print[\[Lambda]]
 
Last edited:
1,688
50
Thanks all! I would have replied a while ago but for some reason I did not receive email notifications. I have it working!
 

Want to reply to this thread?

"Update a vector in a for loop" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top