Use the values in one matrix as index values to another matrix and replace with a value
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello,
I have a matrix A of size 24 x 365 with certain values and I have a matrix B of zeros of the same size as A. I would like to replace the zeros in B with the value 5 in the position mentioned in A.
For example
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/929264/image.png)
I would like B to look like
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/929269/image.png)
I would appreciate any help thank you!
댓글 수: 0
답변 (1개)
David Hill
2022년 3월 16일
The sizes of A and B in your example are not the same.
m=max(A,[],'all');
B=zeros(m,size(A,2));
a=A+(0:m:m*(size(A,2)-1));
B(a)=5;
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!