Update a vector in a for loop

  • Mathematica
  • Thread starter joshmccraney
  • Start date
  • #1
joshmccraney
Gold Member
1,959
102
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]
 

Answers and Replies

  • #2
RPinPA
Science Advisor
Homework Helper
571
319
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}?
 
  • #4
12,219
5,914
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?
 
  • #5
DrClaude
Mentor
7,505
3,786
\[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:
  • Like
Likes mfb and Dale
  • #6
joshmccraney
Gold Member
1,959
102
Thanks all! I would have replied a while ago but for some reason I did not receive email notifications. I have it working!
 

Related Threads on Update a vector in a for loop

Replies
4
Views
771
Replies
2
Views
28K
Replies
4
Views
2K
  • Last Post
Replies
2
Views
8K
  • Last Post
Replies
1
Views
1K
Replies
1
Views
2K
Replies
3
Views
1K
  • Last Post
Replies
2
Views
2K
Top