Modifying entries of a matrix selectively
In my linear algebra class we have a small component of learning Matlab with the occasional assignment.
The question asked to modify an existing (not shown) $25 times 25$ matrix with the following conditions:
- if an entry in the matrix $B$ is $geq 0$ then multiply by $4$
- if an entry in the matrix $B$ is $<0$ then add $6$ to it
I created a function file in Matlab by doing the following:
function [A] = modify_matrix(B, n, m),
A = zeros(n,m);
for i = 1:n,
for j = 1:m,
if B >= 0,
A = B*4;
else,
A = B+6;
A(i,j) = B(i,j);
end
end
end
end
This simply reproduces the same matrix $B$.
Any suggestions?
matlab
add a comment |
In my linear algebra class we have a small component of learning Matlab with the occasional assignment.
The question asked to modify an existing (not shown) $25 times 25$ matrix with the following conditions:
- if an entry in the matrix $B$ is $geq 0$ then multiply by $4$
- if an entry in the matrix $B$ is $<0$ then add $6$ to it
I created a function file in Matlab by doing the following:
function [A] = modify_matrix(B, n, m),
A = zeros(n,m);
for i = 1:n,
for j = 1:m,
if B >= 0,
A = B*4;
else,
A = B+6;
A(i,j) = B(i,j);
end
end
end
end
This simply reproduces the same matrix $B$.
Any suggestions?
matlab
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05
add a comment |
In my linear algebra class we have a small component of learning Matlab with the occasional assignment.
The question asked to modify an existing (not shown) $25 times 25$ matrix with the following conditions:
- if an entry in the matrix $B$ is $geq 0$ then multiply by $4$
- if an entry in the matrix $B$ is $<0$ then add $6$ to it
I created a function file in Matlab by doing the following:
function [A] = modify_matrix(B, n, m),
A = zeros(n,m);
for i = 1:n,
for j = 1:m,
if B >= 0,
A = B*4;
else,
A = B+6;
A(i,j) = B(i,j);
end
end
end
end
This simply reproduces the same matrix $B$.
Any suggestions?
matlab
In my linear algebra class we have a small component of learning Matlab with the occasional assignment.
The question asked to modify an existing (not shown) $25 times 25$ matrix with the following conditions:
- if an entry in the matrix $B$ is $geq 0$ then multiply by $4$
- if an entry in the matrix $B$ is $<0$ then add $6$ to it
I created a function file in Matlab by doing the following:
function [A] = modify_matrix(B, n, m),
A = zeros(n,m);
for i = 1:n,
for j = 1:m,
if B >= 0,
A = B*4;
else,
A = B+6;
A(i,j) = B(i,j);
end
end
end
end
This simply reproduces the same matrix $B$.
Any suggestions?
matlab
matlab
edited Nov 24 '18 at 5:31
Rócherz
2,7762721
2,7762721
asked Nov 21 '18 at 20:40
Forextrader
346
346
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05
add a comment |
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05
add a comment |
2 Answers
2
active
oldest
votes
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
This is what your if-else is missing in your code:
if B(i,j) >= 0,
A(i,j) = 4*B(i,j);
else,
A(i,j) = 6+B(i,j);
end
Those (i,j)
are what enables you to access/modify an entry.
However, as Dmitry pointed out, Matlab enables us to operate over matrices directly (I'm proposing my own variation of Dmitry's one-liner):
A = 4*B.*(aux = (B>=0)) +(B+6).*(1-aux)
add a comment |
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%2f3008319%2fmodifying-entries-of-a-matrix-selectively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
answered Nov 22 '18 at 13:49
Dmitry
633517
633517
add a comment |
add a comment |
This is what your if-else is missing in your code:
if B(i,j) >= 0,
A(i,j) = 4*B(i,j);
else,
A(i,j) = 6+B(i,j);
end
Those (i,j)
are what enables you to access/modify an entry.
However, as Dmitry pointed out, Matlab enables us to operate over matrices directly (I'm proposing my own variation of Dmitry's one-liner):
A = 4*B.*(aux = (B>=0)) +(B+6).*(1-aux)
add a comment |
This is what your if-else is missing in your code:
if B(i,j) >= 0,
A(i,j) = 4*B(i,j);
else,
A(i,j) = 6+B(i,j);
end
Those (i,j)
are what enables you to access/modify an entry.
However, as Dmitry pointed out, Matlab enables us to operate over matrices directly (I'm proposing my own variation of Dmitry's one-liner):
A = 4*B.*(aux = (B>=0)) +(B+6).*(1-aux)
add a comment |
This is what your if-else is missing in your code:
if B(i,j) >= 0,
A(i,j) = 4*B(i,j);
else,
A(i,j) = 6+B(i,j);
end
Those (i,j)
are what enables you to access/modify an entry.
However, as Dmitry pointed out, Matlab enables us to operate over matrices directly (I'm proposing my own variation of Dmitry's one-liner):
A = 4*B.*(aux = (B>=0)) +(B+6).*(1-aux)
This is what your if-else is missing in your code:
if B(i,j) >= 0,
A(i,j) = 4*B(i,j);
else,
A(i,j) = 6+B(i,j);
end
Those (i,j)
are what enables you to access/modify an entry.
However, as Dmitry pointed out, Matlab enables us to operate over matrices directly (I'm proposing my own variation of Dmitry's one-liner):
A = 4*B.*(aux = (B>=0)) +(B+6).*(1-aux)
answered Nov 24 '18 at 5:31
Rócherz
2,7762721
2,7762721
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2f3008319%2fmodifying-entries-of-a-matrix-selectively%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
You said $B*4$, but all that does is return a value. You need to assign it, i.e. $A = B*4$.
– TrostAft
Nov 21 '18 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 '18 at 20:48
Do you mean even if just one entry was positive we multiply whole the matrix by 4 or just do the case with that individual entry?
– Mostafa Ayaz
Nov 21 '18 at 20:56
just the individual entry
– Forextrader
Nov 21 '18 at 21:01
i modified what I posted originally. I was able to produce a matrix that only added 4 when entries were <0 but for some reason it would not multiply by 4 when entry was >=0
– Forextrader
Nov 21 '18 at 21:05