Extracting positive and negative element of a matrix?

21 ビュー (過去 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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by