MATLAB Answers

How do I remove any Column in a matrix

조회 수: 49(최근 30일)
Cutie
Cutie 14 Jan 2021
댓글: Cutie 14 Jan 2021
Please what is wrong with this function. I want it to remove 'n' column form the matrix A
function B = column_removal(A,n)
A1= A(:,1:n-1) % Index from column 1 to column 'n-1'
A2 = A(:,n+1:end) % Index from column 'n+1' to the last co1umn
B = [A1 A2] % concatenate A1 & A2 by which the n column would have been removed
end

  댓글 수: 4

표시 이전 댓글 수: 1
Cutie
Cutie 14 Jan 2021
Can you please explain?
Mathieu NOE
Mathieu NOE 14 Jan 2021
A(:, n) = [];
means that the 'n' th column has been flushed
Cutie
Cutie 14 Jan 2021
Thank you, but how do I assign the above output to a variable.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 14 Jan 2021
편집: Matt J 14 Jan 2021
Nothing is wrong with the code you've posted, but it would be simpler to do,
B=A(:,[1:n-1,n+1:end]);
or, slightly less efficiently,
B=A;
B(:,n)=[];

  댓글 수: 8

표시 이전 댓글 수: 5
Cutie
Cutie 14 Jan 2021
@Matt J, @ercan duzgun & @Walter Roberson thank you so much. I learnt something with your response.
Image Analyst
Image Analyst 14 Jan 2021
And Walter of course!
You can accept the Answer below to give ercan "credit" for helping you in terms of "reputation points". Thanks in advance.
Cutie
Cutie 14 Jan 2021
I wanted to but the 'Accept answer' is not there. That's why I voted it,

댓글을 달려면 로그인하십시오.

추가 답변(1개)

ercan duzgun
ercan duzgun 14 Jan 2021
Dear Cutie,
Your MATLAB function already works fine. However, Matt J's solution is simpler.
I checked your MATLAB function by this example below, and I received the expected result.
X=[1 2 3 4 5 6; 10 20 30 40 5 60;100 200 300 400 500 600]
column_removal(X,3)
A1 =
1 2
10 20
100 200
A2 =
4 5 6
40 5 60
400 500 600
ans =
1 2 4 5 6
10 20 40 5 60
100 200 400 500 600

  댓글 수: 1

Cutie
Cutie 14 Jan 2021
Thank you.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by