Hello,
I am fairly new to MATLAB and I have a question. How do you replace a value in a matrix? So if I have a matrix A, where A=3,2,5,-6. How can I find, and replace all of the negative numbers with 0? So I want to end up with a matrix of 3,2,5,0.
Thank you!
Molly

 채택된 답변

Star Strider
Star Strider 2015년 9월 21일

2 개 추천

One way is to use ‘logical indexing’:
A = [3,2,5,-6];
A(A<0) = 0
A =
3 2 5 0

추가 답변 (1개)

Molly Baltins
Molly Baltins 2015년 9월 21일

0 개 추천

Thank you! This has helped a lot!

댓글 수: 5

Star Strider
Star Strider 2015년 9월 21일
My pleasure!
Molly Baltins
Molly Baltins 2015년 9월 22일
I have another question that hopefully you will be able to help with. I have a string and I want to give each letter a character, but I want it to start off with one. So example I have 'Molly' then it will be [1 2 3 4 5]. Thank you!
My pleasure!
I’m not quite certain what you’re asking, so I’ll take a guess at an answer:
str = 'Molly';
result1 = str(1:5)
result2 = str([3 1 5 2 4])
result1 =
Molly
result2 =
lMyol
I used the colon (:) operator to create the subscript vector in ‘result1’. It is the same as typing [1 2 3 4 5].
Molly Baltins
Molly Baltins 2015년 9월 22일
Perfect! That is exactly what u wanted! Thank you!
Star Strider
Star Strider 2015년 9월 22일
Again, my pleasure!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Text Analytics Toolbox에 대해 자세히 알아보기

질문:

2015년 9월 21일

댓글:

2015년 9월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by