How to clear column and row

3 ビュー (過去 30 日間)
Ayob
Ayob 2013 年 7 月 8 日
I want to clear i column and j row from my matrix which I call it A. How can I do it by MATLAB features?

採用された回答

Shashank Prasanna
Shashank Prasanna 2013 年 7 月 8 日
A = randn(10); % Example random 10x10 matrix
i = 5;
j = 4;
A(:,i) = [];
A(j,:) = [];
This will remove them from A

その他の回答 (1 件)

Evan
Evan 2013 年 7 月 8 日
編集済み: Evan 2013 年 7 月 8 日
What do you mean by "clear"? Do you want to set that value to zero, NaN, etc.? Or remove it entirely?
You can remove an entire row or column in Matlab like so:
A(i,:) = [];
A(:,j) = [];
However, you cannot remove a single element of an array because that would change the dimensions of the array and make it non-rectangular. The reason for this is the same reason that you can run the first line below but not the second one:
A = [1 2 3; 4 5 6; 7 8 9]; %succeeds
A = [1 2 3; 4 5; 7 8 9]; %results in error
A(2,3) = []; %also results in error
If you absolutely had to set a particular in an array to blank or empty, you could use cell arrays. This option really isn't preferred if you can get by with setting the desired element to a value like 0 or NaN, however.
A = num2cell(rand(5));
A{i,j} = [];

カテゴリ

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