how do i find a set using matrix

6 次查看(过去 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.

回答(1 个)

DGM
DGM 2022-6-26
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 个评论
DGM
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]
Q = 6×6
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
Q = 4
Something is missing from this explanation.
Kajal Agrawal
Kajal Agrawal 2022-6-28
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by