Hi, I got a little problem and I hoped someone can help me out.
When I have a matrix consisting of 3 columns, I want to replace a value in the third column if a row in the first column has a certain value. For example, I have the following matrix:
A =
3 6 9
2 6 8
3 6 8
Lets call the first column x, the second y and the third z. Now what I want is that when x == 3, than z must become 20.
How can I do this?

 채택된 답변

Akira Agata
Akira Agata 2018년 11월 25일

0 개 추천

Please try the following:
idx = A(:,1) == 3;
A(idx,3) = 20;

댓글 수: 4

Danny Helwegen
Danny Helwegen 2018년 11월 25일
Dear Akira Agata,
thx for the fast response, is it in this way also possible to say if x == 3 and y == 3 than z must become 40?
Image Analyst
Image Analyst 2018년 11월 25일
편집: Image Analyst 2018년 11월 25일
Of course.
rowsToChange = A(:, 1) == 3 & A(:, 2) == 3;
A(rowsToChange, 3) = 40;
You should really learn how logical indexing works. Since you don't know (yet), you're missing out on one of the most powerful capabilities of MATLAB.
Danny Helwegen
Danny Helwegen 2018년 11월 25일
This works, thank you very much and I will use the tip
Akira Agata
Akira Agata 2018년 11월 25일
> Image Analyst
Thank you for your comment!

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

추가 답변 (0개)

카테고리

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

질문:

2018년 11월 25일

댓글:

2018년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by