필터 지우기
필터 지우기

how can I change in a Matrix 4x7 a certain numbers in the Matrix from positive to negative or vise versa by using ind2sub function.

조회 수: 1 (최근 30일)
how can I change in a Matrix 4x7 a certain numbers in the Matrix from positive to negative or vise versa by using ind2sub function.
For example:
1 2 3 5 4 5 6
3 9 3 0 29 9 8
57 64 2 5 8 1 9
3 8 3 2 4 7 10
To the negative value when the numbers are less than 10 and vice versa.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 1일
편집: Ameer Hamza 2020년 10월 1일
No need to use ind2sub. Just use logical indexing
A(A<10) = -A(A<10);
Another method
idx = find(A < 10);
A(idx) = -A(idx);
And finally: if you really want to use ind2sub()
idx = find(A < 10);
[r, c] = ind2sub(size(A), idx);
for i = 1:numel(r)
A(r(i), c(i)) = -A(r(i), c(i));
end
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 10월 1일
This is the correct syntax if you want to do it like that.
A(ind2sub([4,7],find(A<10))) = -A(find(A<10));
However, it is an inefficient approach; MATLAB will also give a warning.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by