필터 지우기
필터 지우기

how do i find a set using matrix

조회 수: 3 (최근 30일)
Kajal Agrawal
Kajal Agrawal 2022년 6월 26일
댓글: Kajal Agrawal 2022년 6월 28일
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