Filter löschen
Filter löschen

How to delete a submatrix from a matrix using the ZEROS command instead of [ ] square brackets?

5 Ansichten (letzte 30 Tage)
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."

Antworten (4)

Matt J
Matt J am 18 Sep. 2015
  2 Kommentare
Matt J
Matt J am 18 Sep. 2015
Bearbeitet: Matt J am 18 Sep. 2015
this is the question for our class: Q1: Delete a submatrix from a matrix using the command zeros (instead of [ ]).
Is this a smart-aleck answer? :)
B=A(1:length(zeros(1,2)):3,:)

Melden Sie sich an, um zu kommentieren.


Kirby Fears
Kirby Fears am 18 Sep. 2015
Bearbeitet: Kirby Fears am 18 Sep. 2015
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 Kommentare
Kevin Lai
Kevin Lai am 18 Sep. 2015
this is the question for our class: Q1: Delete a submatrix from a matrix using the command zeros (instead of [ ]).
arich82
arich82 am 18 Sep. 2015
Bearbeitet: arich82 am 18 Sep. 2015
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

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 18 Sep. 2015
Bearbeitet: Matt J am 18 Sep. 2015
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;

James Tursa
James Tursa am 18 Sep. 2015
Bearbeitet: James Tursa am 18 Sep. 2015
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.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by