Extracting positive and negative element of a matrix?

15 visualizaciones (últimos 30 días)
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 ?

Respuesta aceptada

Julius Muschaweck
Julius Muschaweck el 8 de Sept. de 2021
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(:))

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by