Extracting positive and negative element of a matrix?

조회 수: 15 (최근 30일)
Amit Chakraborty
Amit Chakraborty 2021년 9월 8일
답변: Julius Muschaweck 2021년 9월 8일
Suppose; A= 3D Matrix of size (10,10,10) , where I have some values in positive and some values in negative.
So, with that positive value element I want to multiply it with -1000 and with the negative value element of the matrix A I want to multiply it with 100 and after doing this scaling I want to save them in a new matrix. Can any one help me ?

채택된 답변

Julius Muschaweck
Julius Muschaweck 2021년 9월 8일
Use logical indexing:
A = rand(10,10, 10) - 0.5; % contains random numbers in [-0.5, 0.5]
B = zeros(10, 10, 10); % preallocating the new matrix.
Aneg = A < 0;
Apos = A > 0; % Aneg and Apos are 10x10x10 logical matrices
B(Apos) = A(Apos) * (-1000); % which you can use to index into an array
B(Aneg) = A(Aneg) * 100;
histogram(B(:))

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by