Rounding off components of a matrix

1 回表示 (過去 30 日間)
Nusti
Nusti 2012 年 6 月 19 日
Hello, I am new to matlab and I got stuck at a particular point.
I have a 256 x 904 matrix with entries between -1 and 1. I need to round it off in such a way that for entries less than or equal to 0, the output should be -1 and for entries greater than 0, the output should be 1.
So, I named this matrix X.
How do I denote X = (xij) in Matlab?
I was thinking of the following code:
if xij < = 0 xij= -1 else xij = 1 end
Could someone please help me?

回答 (2 件)

the cyclist
the cyclist 2012 年 6 月 19 日
This particular operation is easy to do across the entire matrix at once:
x = sign(x);
Even if there had not been a simple function that happened to do exactly what you needed, you can often do rounding operations cleverly on the whole matrix.
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 19 日
X(i,j)
However, you do not need that representation for this purpose: sign(X) will do the entire matrix at once.
Walter Roberson
Walter Roberson 2012 年 6 月 19 日
Note: sign(0) is 0, whereas Nusti requested -1 as the output for 0.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 6 月 19 日
roundedX = (X > 0) * 2 - 1;
This will do the entire matrix at one time.
Another approach:
roundedX = ones(size(X));
roundedX(X <= 0) = -1;

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by