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

A(:, n) = [];
Cutie
Cutie 2021년 1월 14일
Can you please explain?
Mathieu NOE
Mathieu NOE 2021년 1월 14일
A(:, n) = [];
means that the 'n' th column has been flushed
Cutie
Cutie 2021년 1월 14일
Thank you, but how do I assign the above output to a variable.

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

 채택된 답변

Matt J
Matt J 2021년 1월 14일
편집: Matt J 2021년 1월 14일

2 개 추천

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

Cutie
Cutie 2021년 1월 14일
Thank you @Matt J but the function still shows error when I ran it....It's displaying 'Not enough input arguments.
B = A(:, setdiff(1:end, n))
is another way to write it that is sometimes easier to think about, especially once you start getting multiple exclusions.
Cutie
Cutie 2021년 1월 14일
Thank you @Matt J @Walter Roberson & @ercan duzgunfor your response. I am newbie to MATLAB, so plase tolerate my seeming elementary questions, I know the line of code (I wrote above work). But I created it as a function & saved. When I open the function and click run, this is when I get this response 'Not enough input arguments.'
You opened the function in the editor and you clicked on the green Run button. When you did that, MATLAB had no idea what A and n should refer to.
You need to go down to the command line and pass in values, such as
A = magic(12)
B = column_removal(A, 4)
ercan duzgun
ercan duzgun 2021년 1월 14일
Cutie, you can't use MATLAB "function" by clicking "run" button. After creating and saving the function (file name have to the same function name. For your case, file name have to be "column_removal.m"), just close it. You can call and use the function in your program scripts now. For example, in command window: just type the command of
>>X=[1 2 3 4 5 6; 10 20 30 40 5 60;100 200 300 400 500 600]
>> column_removal(X,3)
Or you can use the funciton in your another MATLAB programme script file. Run button works with MATLAB "script" files, but not with "function" files.
Cutie
Cutie 2021년 1월 14일
편집: Cutie 2021년 1월 14일
@Matt J, @ercan duzgun & @Walter Roberson thank you so much. I learnt something with your response.
Image Analyst
Image Analyst 2021년 1월 14일
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 2021년 1월 14일
I wanted to but the 'Accept answer' is not there. That's why I voted it,

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

추가 답변 (1개)

ercan duzgun
ercan duzgun 2021년 1월 14일

2 개 추천

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2021년 1월 14일

댓글:

2021년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by