Converting location of a 2x3 vector into a matrix with value 1

조회 수: 1 (최근 30일)
Wietze Zijpp
Wietze Zijpp 2022년 3월 22일
편집: Bruno Luong 2022년 3월 23일
Suppose I have
a = 2×3
1 3
2 4
7 8
Now I want to create a matrix of dimension 10 x 10 where the entries 1,3 and 2,4 and 7,8 are equal to one.
Z = zeros(10) % 10 x 10 matrix containing only zeros
Z(a(1,1),a(1,2))=1 % now entry 1,3 is equal to 1
This is an illustrative example and I could for sure just code the second line three times. However for a large matrix a this will be tedious. I have tried to solve this problem with some for loops but without any positve result.
  댓글 수: 3
Wietze Zijpp
Wietze Zijpp 2022년 3월 23일
Thank you for your response. It is more than the output I expect. I would like to only obtain the output
Z(a(1,1),a(1,2))=1; % so entry 1.3 =1
Z(a(2,1),a(2,2))=1; % entry 2,4 = 1
Z(a(3,1),a(3,2))=1; % entry 7,8 = 1
Arif Hoq
Arif Hoq 2022년 3월 23일
or this one ?
a = [1 3;2 4;7 8];
Z = zeros(10) ;% 10 x 10 matrix containing only zeros
Z(a(1,1),a(1,2))=1; % now entry 1,3 is equal to 1
Z(a(2,1),a(2,2))=1;
Z(a(3,1),a(3,2))=1
Z = 10×10
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 3월 23일
편집: Voss 2022년 3월 23일
a = [1 3
2 4
7 8];
Z = zeros(10,10);
Z(sub2ind(size(Z),a(:,1),a(:,2))) = 1
Z = 10×10
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

추가 답변 (3개)

Arif Hoq
Arif Hoq 2022년 3월 23일
편집: Arif Hoq 2022년 3월 23일
if 2nd one is your expecter output , then
a = [1 3;2 4;7 8];
Z = zeros(10) ;% 10 x 10 matrix containing only zeros
row=size(a,1);
j=1:2;
for i=1:row
Z(a(i,j(1)),a(i,j(2)))=1;
end

Stephen23
Stephen23 2022년 3월 23일
편집: Stephen23 2022년 3월 23일
"However for a large matrix a this will be tedious."
If you have a large matrix it may be better if it were a sparse array (which can make operations using it more efficient), in which case this task is very easy:
a = [1,3;2,4;7,8];
m = sparse(a(:,1),a(:,2),1,10,10)
m =
(1,3) 1 (2,4) 1 (7,8) 1
full(m) % checking
ans = 10×10
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Bruno Luong
Bruno Luong 2022년 3월 23일
편집: Bruno Luong 2022년 3월 23일
a = [1,3;2,4;7,8]; % assumed there is no repeated indexes
A = accumarray(a,1,[10,10])
A = 10×10
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by