Exclusion of Columns in a matrix

1 回表示 (過去 30 日間)
Alan
Alan 2012 年 1 月 3 日
My question is: I want to delete columns that possess the sum equal 1, however, when I use the function "for" occurs a problem....For each time that is deleted a column, the matrix is decreasing the number of columns, so that index used above exceed size of matrix....how can I solve this problem?
for j=1:794;
if S(j,1)==1;
X(:,j)=[];
end
end
when perform this command is showed:
??? Index of element to remove exceeds matrix dimensions.
thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 1 月 3 日
X = X(:,~abs(sum(X) - 1)<1e3*eps);
OR
S = sum(X);
X = X(:,~abs(S - 1)<1e3*eps);
OR
X = X(:,abs(sum(X) - 1)>1e3*eps);
e.g.
>> X = randi(23,5)
X =
7 12 18 23 20
16 23 6 13 6
16 8 12 4 19
4 14 17 4 6
3 6 21 6 22
>> X(:,3) = .2
X =
7 12 0.2 23 20
16 23 0.2 13 6
16 8 0.2 4 19
4 14 0.2 4 6
3 6 0.2 6 22
>> X = X(:,abs(sum(X) - 1)>1e3*eps)
X =
7 12 23 20
16 23 13 6
16 8 4 19
4 14 4 6
3 6 6 22
>>
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 1 月 3 日
in this is case not need use loops "for..end".
just use e.g.:
X = X(:,abs(sum(X) - 1)>1e3*eps);
Andrei Bobrov
Andrei Bobrov 2012 年 1 月 3 日
see e.g.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by