Logical indexing and for loop question Matlab
I have a vector p=[-3 -2 -1 0 1 2 3]
, and an expression like
$psi_p=a~e^{ikp}+b~e^{-ikp}$ for $-3leq p< 1$ and $psi_p=c~e^{ikp}$ for $1leq pleq 3$; $a$, $b$ and $c$ have values which are given in my code below. The first issue is related to the logical indexing because p
has negative and zero values. Secondly, how to write a for
loop to implement the above statement? I want to create a vector $[psi_{-3},psi_{-2},~...~,psi_2,psi_3]$. The code I wrote is
k=2; p=[-3 -2 -1 0 1 2 3]; c=1; a=5; b=9;
psi3=c*exp(3*i*k);
psi2=c*exp(2*i*k);
psi_33 = a*exp(-1i*3*k)+b*exp(1i*3*k);
psi_22 = a*exp(-1i*2*k)+b*exp(1i*2*k);
psi_11 = a*exp(-1i*1*k)+b*exp(1i*1*k);
psi_0 = a+b;
psi_1 = c*exp(1i*k);
psi_2 = c*exp(1i*2*k);
psi_3 = c*exp(1i*3*k);
phi11=[psi_33,psi_22,psi_11,psi_0,psi_1,psi_2,psi_3]
Just looking for a more clean way to write so I can manipulate for even very large p
say in 1000s. Thanks.
numerical-methods matlab numerical-linear-algebra
add a comment |
I have a vector p=[-3 -2 -1 0 1 2 3]
, and an expression like
$psi_p=a~e^{ikp}+b~e^{-ikp}$ for $-3leq p< 1$ and $psi_p=c~e^{ikp}$ for $1leq pleq 3$; $a$, $b$ and $c$ have values which are given in my code below. The first issue is related to the logical indexing because p
has negative and zero values. Secondly, how to write a for
loop to implement the above statement? I want to create a vector $[psi_{-3},psi_{-2},~...~,psi_2,psi_3]$. The code I wrote is
k=2; p=[-3 -2 -1 0 1 2 3]; c=1; a=5; b=9;
psi3=c*exp(3*i*k);
psi2=c*exp(2*i*k);
psi_33 = a*exp(-1i*3*k)+b*exp(1i*3*k);
psi_22 = a*exp(-1i*2*k)+b*exp(1i*2*k);
psi_11 = a*exp(-1i*1*k)+b*exp(1i*1*k);
psi_0 = a+b;
psi_1 = c*exp(1i*k);
psi_2 = c*exp(1i*2*k);
psi_3 = c*exp(1i*3*k);
phi11=[psi_33,psi_22,psi_11,psi_0,psi_1,psi_2,psi_3]
Just looking for a more clean way to write so I can manipulate for even very large p
say in 1000s. Thanks.
numerical-methods matlab numerical-linear-algebra
1
Try this:psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41
add a comment |
I have a vector p=[-3 -2 -1 0 1 2 3]
, and an expression like
$psi_p=a~e^{ikp}+b~e^{-ikp}$ for $-3leq p< 1$ and $psi_p=c~e^{ikp}$ for $1leq pleq 3$; $a$, $b$ and $c$ have values which are given in my code below. The first issue is related to the logical indexing because p
has negative and zero values. Secondly, how to write a for
loop to implement the above statement? I want to create a vector $[psi_{-3},psi_{-2},~...~,psi_2,psi_3]$. The code I wrote is
k=2; p=[-3 -2 -1 0 1 2 3]; c=1; a=5; b=9;
psi3=c*exp(3*i*k);
psi2=c*exp(2*i*k);
psi_33 = a*exp(-1i*3*k)+b*exp(1i*3*k);
psi_22 = a*exp(-1i*2*k)+b*exp(1i*2*k);
psi_11 = a*exp(-1i*1*k)+b*exp(1i*1*k);
psi_0 = a+b;
psi_1 = c*exp(1i*k);
psi_2 = c*exp(1i*2*k);
psi_3 = c*exp(1i*3*k);
phi11=[psi_33,psi_22,psi_11,psi_0,psi_1,psi_2,psi_3]
Just looking for a more clean way to write so I can manipulate for even very large p
say in 1000s. Thanks.
numerical-methods matlab numerical-linear-algebra
I have a vector p=[-3 -2 -1 0 1 2 3]
, and an expression like
$psi_p=a~e^{ikp}+b~e^{-ikp}$ for $-3leq p< 1$ and $psi_p=c~e^{ikp}$ for $1leq pleq 3$; $a$, $b$ and $c$ have values which are given in my code below. The first issue is related to the logical indexing because p
has negative and zero values. Secondly, how to write a for
loop to implement the above statement? I want to create a vector $[psi_{-3},psi_{-2},~...~,psi_2,psi_3]$. The code I wrote is
k=2; p=[-3 -2 -1 0 1 2 3]; c=1; a=5; b=9;
psi3=c*exp(3*i*k);
psi2=c*exp(2*i*k);
psi_33 = a*exp(-1i*3*k)+b*exp(1i*3*k);
psi_22 = a*exp(-1i*2*k)+b*exp(1i*2*k);
psi_11 = a*exp(-1i*1*k)+b*exp(1i*1*k);
psi_0 = a+b;
psi_1 = c*exp(1i*k);
psi_2 = c*exp(1i*2*k);
psi_3 = c*exp(1i*3*k);
phi11=[psi_33,psi_22,psi_11,psi_0,psi_1,psi_2,psi_3]
Just looking for a more clean way to write so I can manipulate for even very large p
say in 1000s. Thanks.
numerical-methods matlab numerical-linear-algebra
numerical-methods matlab numerical-linear-algebra
asked Nov 28 '18 at 5:23
AtoZAtoZ
155
155
1
Try this:psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41
add a comment |
1
Try this:psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41
1
1
Try this:
psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
Try this:
psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3016744%2flogical-indexing-and-for-loop-question-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3016744%2flogical-indexing-and-for-loop-question-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try this:
psi = (p < 1) .* (a * exp(1i*k*p) + b * exp(-1i*k*p)) + (p >= 1) .* (c * exp(1i*k*p));
– Algebraic Pavel
Nov 28 '18 at 9:22
@AlgebraicPavel This works :-) Thanks.. So there's no need to use logical indexing or loops etc?
– AtoZ
Nov 28 '18 at 10:41