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

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 日

1 投票

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 日
Thank you very much Mr. Hamza.
Basically, Vector A represents a network of 10 nodes and vector B represents a network of 100 nodes. I want to connect each node of A with 10 different nodes of B. The rows of Matrix C are nodes of A and its columns are nodes of B. If there is a link between node 2 in A with node 4 in B, I will insert one in C(2,4) and 0 if there is no link.
Asaf McRock
Asaf McRock 2020 年 11 月 29 日
Is there a tutorial about cells in MATLAB? I'm very new to computer programming and I would like to study the topic of cells and structures.
Ameer Hamza
Ameer Hamza 2020 年 11 月 29 日
Do you want matrix C to only have 1s and 0s. In that case, try this
A = 1:10;
B = 1:100;
n = numel(B)/numel(A);
C = repmat({ones(1,n)}, size(A));
C = blkdiag(C{:})
You can take this free course: https://www.mathworks.com/learn/tutorials/matlab-onramp.html to learn the basics of MATLAB programming.
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 件)

カテゴリ

質問済み:

2020 年 11 月 29 日

コメント済み:

2020 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by