フィルターのクリア

how to use index referencing ?

2 ビュー (過去 30 日間)
Islam
Islam 2014 年 2 月 27 日
回答済み: Sean de Wolski 2014 年 2 月 27 日
I have A =
[1 -2 1 0.7 3
1 -0.1 -3 2 -0.2
0 0 1 1 -2 ]
I want to choose the most negative of each column, and drop the rest. If a column doesn't have negative values then it is zero. Thus, the above matrix will be
A =
[ 0 -2 0 0 0
0 0 -3 0 0
0 0 0 0 -2 ]
Also, I want to keep the values of the same index in the x matrix.
x =
[ 440 680 520 240 730
440 680 520 240 730
440 680 520 240 730 ]
. Thus, x will be
x =
[ 0 680 0 0 0
0 0 520 0 0
0 0 0 0 730 ]
Thanks for your quick responses,

回答 (2 件)

Jos (10584)
Jos (10584) 2014 年 2 月 27 日
Assuming A and X are your input arrays
szA = size(A)
B = zeros(szA)
[minA,Rix] = min(A,[],1) % minimum value per column, and the row index
LinIDX = sub2ind(size(A), Rix, 1:szA(2))
B(LinIDX) = minA
B = min(B,0) % for cases when all elements in a column of A are positive
X2 = (B<0) .* X
  2 件のコメント
Islam
Islam 2014 年 2 月 27 日
but this answer doesn't drop negative values if they are not the minimum in the column.
Each column should he one value only. but in your solution I have more than one value ..
Jos (10584)
Jos (10584) 2014 年 2 月 27 日
The result is stored in variable B not in A. Each column of B holds a single value, namely the minimum value in that column of A, as you requested. If you want to have it stored in A, you can always execute:
A = B

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


Sean de Wolski
Sean de Wolski 2014 年 2 月 27 日
A = [1 -2 1 0.7 3
1 -0.1 -3 2 -0.2
0 0 1 1 -2 ];
x = rand(size(A));
B = bsxfun(@times,bsxfun(@eq,A,min(A,[],1)),min(A,0));
x(B==0) = 0;

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by