compare two values and chosing the higher one

28 ビュー (過去 30 日間)
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 23 日
編集済み: Andrei Bobrov 2017 年 11 月 23 日
user = 2;
subcarrier = 4;
randn(user,subcarrier)
If i run this i am getting
ans =
-0.1623 -0.5320 -0.8757 -0.7120
-0.1461 1.6821 -0.4838 -1.1742
from the values i want ans to be displayed as
-0.1623 0 -0.8757 -0.7120
0 1.6821 0 0.
how can i modify the code to get this output
  1 件のコメント
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 23 日
編集済み: Walter Roberson 2017 年 11 月 23 日
0 0 0 -0.7120
-0.1461 1.6821 -0.4838 0
for this output i want to modify the code

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

回答 (3 件)

KSSV
KSSV 2017 年 11 月 23 日
A = [-0.1623 -0.5320 -0.8757 -0.7120
-0.1461 1.6821 -0.4838 -1.1742];
B = [-0.1623 0 -0.8757 -0.7120
0 1.6821 0 0.] ;
C = zeros(size(A)) ;
[val,idx] = max(abs(A)) ;
% [I_row, I_col] = ind2sub(size(A),idx) ;
% C(I_row,I_col) = val ;
for i = 1:size(A,2)
[val,idx] = max(abs(A(:,i))) ;
C(idx,i) = val ;
end
  1 件のコメント
Prabha Kumaresan
Prabha Kumaresan 2017 年 11 月 23 日
Could you tell me how to modify the code if A has 10 rows and 64 columns of values instead of [-0.1623 -0.5320 -0.8757 -0.7120 -0.1461 1.6821 -0.4838 -1.1742];

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


Walter Roberson
Walter Roberson 2017 年 11 月 23 日
user = 2;
subcarrier = 4;
temp = randn(user,subcarrier);
temp( bsxfun(@lt, temp, max(temp)) ) = 0
Note: your requirement is not defined in the case where there are duplicate maximum values.
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 11 月 23 日
My code is already set up to give only one non-zero entry per column. (All of the entries in a column could be 0 if it happened that randn produced an exact 0!)

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


Andrei Bobrov
Andrei Bobrov 2017 年 11 月 23 日
編集済み: Andrei Bobrov 2017 年 11 月 23 日
a = [ -0.1623 -0.5320 -0.8757 -0.7120
-0.1461 1.6821 -0.4838 -1.1742];
out = (a == max(a)).*a;
or
out = a.*bsxfun(@eq,max(a),a);

カテゴリ

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