Extracting positive and negative element of a matrix?

19 visualizzazioni (ultimi 30 giorni)
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 ?

Risposta accettata

Julius Muschaweck
Julius Muschaweck il 8 Set 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(:))

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by