how do i find a set using matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
DGM
2022-6-26
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 个评论
DGM
2022-6-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!