assign values from logial vector to a new matrix with the same size

조회 수: 1 (최근 30일)
james berry
james berry 2018년 2월 25일
답변: Jos (10584) 2018년 3월 8일
function B = functionA(A)
B = zeros(size(A));
D= A>0;
B = A(D);
I want to assign the values of A that met the condition (>0) to a matrix B of the same size as A. I wand a matrix B with zeros, the same size as A and the values of matrix A. Does anyone has a hint how to do this?

답변 (2개)

Arthur Goldsipe
Arthur Goldsipe 2018년 3월 7일
편집: Arthur Goldsipe 2018년 3월 8일
You can use an indexing variable like D on both the left and right side of an assignment. If I understand your questions, you can get what you want by changing the last line of your sample code to the following:
B(D) = A(D);

Jos (10584)
Jos (10584) 2018년 3월 8일
function B = functionA(A)
B = A
B(~(A>0)) = 0

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by