필터 지우기
필터 지우기

How to form a binary matrix from a given two vectors?

조회 수: 1 (최근 30일)
Asaf McRock
Asaf McRock 2020년 11월 29일
댓글: Ameer Hamza 2020년 11월 29일
Hi everyone,
Say I have two vectors with two different sizes:
Vector A has ten positive integers ( 1:10) and vector B has 100 positive integers (1:100). I am planning to create a matrix where its size is 10 by 100.
The row picture of matrix C:
the first raw has ones values in the 1-10 columns and zeros elsewhere.
the second raw has ones values in the 11-20 columns and zeros elsewhere.
the third raw has ones values in the third 21-30 columns and zeros elsewhere.
and so on for the other raws in the same pattern.
Your ideas and help would be greatly appreciated!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 29일
편집: Ameer Hamza 2020년 11월 29일
I am not sure how the elements of B are used in C. Consider following code
A = 1:10;
n = 10;
C = arrayfun(@(x) {repmat(x, 1, n)}, A);
C = blkdiag(C{:})
or
C = mat2cell(repelem(A, 1, n), 1, n*ones(size(A)));
C = blkdiag(C{:})
  댓글 수: 5
Asaf McRock
Asaf McRock 2020년 11월 29일
WOW! Thank you very much, Mr. Hamza! This is exactly what I'm looking for.
Ameer Hamza
Ameer Hamza 2020년 11월 29일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by