フィルターのクリア

I want to create a binary matrix with a variable dimensions

4 ビュー (過去 30 日間)
Nir Vaishnav
Nir Vaishnav 2022 年 8 月 11 日
コメント済み: David Hill 2022 年 8 月 11 日
I want to create a binary matrix of dimension k x M where k and M are user defined.(k is the number of rows and M is the number of column). And number of zeros and ones in each row are user defined. Also this binary matrix should be a function.
  3 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 8 月 11 日
Seems like home work/assignment.
Check out
Nir Vaishnav
Nir Vaishnav 2022 年 8 月 11 日
for kk=1:lnk
m = input('How many zeroes do you need \n');
n = input('how many ones do you need\n');
lnk=nchoosek(m+n,n);
i=n+m;
if i<=1
disp('size of binary sequence is out of range');
else
%Binary sequence contains m zeros and n ones in any order
x1=zeros(1,m);
x1(randn(i,n))=1
%count down the number of switches in such a Binary sequence x1;
end
end
This is what i have done but it creates only arrays i dont understand how to make it into a matrix. and then convert into a function

サインインしてコメントする。

回答 (1 件)

David Hill
David Hill 2022 年 8 月 11 日
編集済み: David Hill 2022 年 8 月 11 日
k=10;
M=50;
O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)
for n=1:k
m=[ones(1,O(n)),zeros(1,M-O(n))];
Matrix(n,:)=m(randperm(M));
end
  2 件のコメント
Nir Vaishnav
Nir Vaishnav 2022 年 8 月 11 日
if i change the value of k,M then it shows the error that left side matrix dimension is not equal to right side.
David Hill
David Hill 2022 年 8 月 11 日
You just need to clear Matrix or set Matrix = [ ] at the beginning.
k=10;
M=50;
Matrix=[];
O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)
for n=1:k
m=[ones(1,O(n)),zeros(1,M-O(n))];
Matrix(n,:)=m(randperm(M));
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by