how do i find a set using matrix

2 views (last 30 days)
Kajal Agrawal
Kajal Agrawal on 26 Jun 2022
Commented: Kajal Agrawal on 28 Jun 2022
i have a matrix and also lower and upper bound for it.
firstly the set is empty and i want to inclue the matrix element a11 in a set. then reduces matrix using the following command,
Q(Q(1,:)==1,:)=[]
>> Q(:,Q(1,:)==1) = []
>> Q(:,1)=[];
>> Q(1,:)=[];
after that again add a11 element in a set..so i have a set...and this process done untill the matrix is null.

Answers (1)

DGM
DGM on 26 Jun 2022
Like this?
sza = 10;
A = randi([1 10],sza)
A = 10×10
3 6 2 5 5 8 8 4 9 8 4 1 8 6 8 5 10 9 9 9 7 7 1 10 4 6 2 10 3 8 1 3 9 9 10 10 8 4 3 9 1 4 4 5 9 9 7 1 2 5 10 3 3 2 8 8 4 10 2 10 6 4 1 3 7 4 6 3 4 3 1 3 3 9 4 3 1 2 9 8 5 7 9 9 3 3 7 3 6 6 1 10 5 9 10 6 7 5 2 7
% output will be variable-length
% preallocate to maximum length
kmax = max(size(A,1),size(A,2));
outvec = zeros(1,kmax);
k = 0;
while ~isempty(A)
outvec(k+1) = A(1,1);
k = k+1;
A(A(1,:)==1,:) = [];
if isempty(A); break; end
A(:,A(1,:)==1) = [];
% it's not clear if these cases should still be used
% if the first row/column has already been deleted in the prior lines
if isempty(A); break; end
A(:,1) = [];
if isempty(A); break; end
A(1,:) = [];
end
% crop off excess vector length
outvec = outvec(1:k)
outvec = 1×8
3 1 9 9 8 6 6 7
That might simplify, but it still seems too ambiguous to bother optimizing.
  3 Comments
Kajal Agrawal
Kajal Agrawal on 28 Jun 2022
Yes you are right, perhaps i'm not able to gives you a proper explain, Q(1,1)=4 and i want to counting this element in a set.

Sign in to comment.

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!