Modifying entries of a matrix selectively
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 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 at 20:56
just the individual entry
– Forextrader
Nov 21 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 at 21:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 5:31
Rócherz
2,7062721
2,7062721
asked Nov 21 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 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 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 at 20:56
just the individual entry
– Forextrader
Nov 21 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 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 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 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 at 20:56
just the individual entry
– Forextrader
Nov 21 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 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 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 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 at 20:48
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 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 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 at 20:56
just the individual entry
– Forextrader
Nov 21 at 21:01
just the individual entry
– Forextrader
Nov 21 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 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 at 21:05
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
up vote
0
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
up vote
0
down vote
Using Matlab vectorization tricks you can do that in one line:
A=B.*(B>0)*4+(B+6).*(B<0)
add a comment |
up vote
0
down vote
up vote
0
down vote
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 at 13:49
Dmitry
604517
604517
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 5:31
Rócherz
2,7062721
2,7062721
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 at 20:44
Thanks, I changed that but it still is not modifying matrix B
– Forextrader
Nov 21 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 at 20:56
just the individual entry
– Forextrader
Nov 21 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 at 21:05