How to delete a submatrix from a matrix using the ZEROS command instead of [ ] square brackets?
4 次查看(过去 30 天)
显示 更早的评论
I have a 3x3 matrix i.e. A=[5,2,6; -1,3,4; 0,5,3] and if I want to delete my middle row, how do I delete that without using the square brackets? I realized zeros=(0) is the equivalent of a square bracket. But when I tried to substitute the second script i.e.
>> B=zeros(0)
B =
[]
>> A(2,1:3) = B
and I get an error message of "Subscripted assignment dimension mismatch."
0 个评论
回答(4 个)
Kirby Fears
2015-9-18
编辑:Kirby Fears
2015-9-18
Hi Kevin,
Using the square bracket assignment is the standard way to delete a row or column. I'm curious why you're looking for another method.
Anyway, you can avoid the square brackets this way:
m=magic(3);
m=m([1 3:end],:);
With a larger matrix m, you can delete the kth row (where k is not the first or last row) as follows:
m=m([1:k-1 k+1:end],:);
The typical square brackets method is easier to write:
m=magic(3);
m(2,:)=[];
Hope this helps.
4 个评论
arich82
2015-9-18
编辑:arich82
2015-9-18
The following is a stupid, stupid way of doing things, but if you insist on interpreting the question literally, you could wrap a call to zeros inside of a call to logical to create a mask:
>> A = [...
5, 2, 6; ...
-1, 3, 4; ...
0, 5, 3; ...
];
>> A(logical([1, zeros(1), 1]), :)
ans =
5 2 6
0 5 3
My guess, however, is that James Tursa's interpretation is the correct one, and that "delete" really implies "set to zero":
>> A(2, :) = zeros(size(A(2, :)))
A =
5 2 6
0 0 0
0 5 3
Note that, in general, you cannot "delete" an arbitrary submatrix from a matrix and still be left with a matrix; it only works if you delete (multiples of) full rows xor full columns. You can, however, set arbitrary submatrices to zero:
>> B = reshape(1:9, 3, 3)
B =
1 4 7
2 5 8
3 6 9
>> B(1:2, 1:2) = []
Subscripted assignment dimension mismatch.
>> B(1:2, 1:2) = zeros(2, 2)
B =
0 0 7
0 0 8
3 6 9
You can apply a mask to delete an arbitrary submatrix, but the result is a linear-indexed vector:
>> C = reshape(1:9, 3, 3)
C =
1 4 7
2 5 8
3 6 9
>> (logical([zeros(2, 2), ones(2, 1); ones(1, 3)]))
ans =
0 0 1
0 0 1
1 1 1
>> C(logical([zeros(2, 2), ones(2, 1); ones(1, 3)]))
ans =
3
6
7
8
9
Matt J
2015-9-18
编辑:Matt J
2015-9-18
I'm guessing you've been asked not to remove the row, but rather to set all the row's elements to zero. Otherwise, I can't see what motivates the use of the ZEROS command specifically. It is possible to set the middle row to zero using the ZEROS command as follows,
A(2,:)=zeros(1,3);
but even that is very artificial and unnecessary. You could simply do,
A(2,:)=0;
0 个评论
James Tursa
2015-9-18
编辑:James Tursa
2015-9-18
Using explicit brackets [ ] on the right hand side of an equation combined with using indexing on the left hand side variable is a special MATLAB syntax that means "remove elements". It is NOT the same thing as an assignment where you have a variable on the right hand side. E.g.,
A = [5,2,6; -1,3,4; 0,5,3];
A(2,1:3) = zeros(0); % <-- This is an assignment with a variable on the rhs (generates an error)
A(2,1:3) = []; % <-- This is a special syntax that tells MATLAB you want to remove elements
In the last code line above, the [ ] on the right hand side combined with indexing on the left hand side is an instruction to the MATLAB parser that you are using a special syntax to remove elements from the left hand side variable. It is NOT treated the same as using zeros(0) on the right hand side (or using a variable that was set to zeros(0)). You need to type the brackets [ ] explicitly to get the special treatment.
If you are simply looking for another coding way to eliminate some elements that mimics the use of [ ] on the rhs, then see Matt J's first answer.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!