Replace some elements in a Matrices with brief code.

The variables which can be copy/paste to Matlab.
a = [5 6 7 9 1 2 5 6 3 8 9 0];
A = [1 3 6 2;2 4 1 9; 2 3 5 6];
B = a(A);
From the variables above,
Matrix B is [5 7 2 6; 6 9 5 3; 6 7 1 2]. However, Actually the condition is set to >=6 which are replaced by the element in Matrix A. So, the desired result is:
DesiredResult = [5 3 2 2; 2 4 5 3; 2 3 1 2];

댓글 수: 2

Guillaume
Guillaume 2019년 3월 8일
편집: Guillaume 2019년 3월 8일
Don't use looping/for. But, can use other function such as "find".
Where do these requirements come from? Is this homework?
Actually the condition is set to >=6 which are replaced by the element in Matrix A
Anyway, you've pretty much written the one line required with that statement. find is not even necessary.
it is not a homework.. I just wanna use simple code.

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

 채택된 답변

Stephen23
Stephen23 2019년 3월 8일

1 개 추천

>> a = [5,6,7,9,1,2,5,6,3,8,9,0];
>> x = [1,3,6,2;2,4,1,9;2,3,5,6];
>> b = a(x);
>> y = b>=6;
>> b(y) = x(y)
b =
5 3 2 2
2 4 5 3
2 3 1 2

추가 답변 (0개)

카테고리

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

제품

릴리스

R2018a

질문:

2019년 3월 8일

편집:

2019년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by