how do i find a set using matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
DGM
am 26 Jun. 2022
Like this?
sza = 10;
A = randi([1 10],sza)
% 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)
That might simplify, but it still seems too ambiguous to bother optimizing.
3 Kommentare
DGM
am 27 Jun. 2022
If Q is:
Q=[4 1 1 0 1 1;
1 4 0 1 1 1;
1 0 4 1 1 1;
0 1 1 4 1 1;
1 1 1 1 4 0;
1 1 1 1 0 4]
Then what is A11? I assume you meant Q(1,1), because there's otherwise no other mention of an array called A or a variable called A11. If you meant Q(1,1), then why is S set to 1?
As you say, after doing those operations, Q has been reduced to a scalar 4. If this element (4) is added to the set, then why is S = 2?
Q(Q(1,:)==1,:)=[];
Q(:,Q(1,:)==1) = [];
Q(:,1)=[];
Q(1,:)=[];
Q
Something is missing from this explanation.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!