Extracting positive and negative element of a matrix?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amit Chakraborty
am 8 Sep. 2021
Beantwortet: Julius Muschaweck
am 8 Sep. 2021
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 ?
0 Kommentare
Akzeptierte Antwort
Julius Muschaweck
am 8 Sep. 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(:))
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!