Good day,
I am new here and also not a profesional matlab user. I am tring to help my friend with her home work. She get a task to create a function which will create matix according to given numbers, m - rows, n - columns. This is simple to do by using to for loops. The problem is that teacher at least what I understood wants to following thing. He wrote: "We divide each row into m-groups of n-numbers, further we fill up all with zeros beside the group "j" in each group row of "i"." Hard to explain. But in first group in first row should be ones. In second group of second row should be ones... etc.
Leter from m to m+n rows he want to fill all with zeros and only "n" number in each grup should be 1. Crazy task for me.
I am trying to find some solution for that by I cannot. I was trying to look for some subgoruping in matrix. But without luck.
The result shoul look like that:
What i did already is just:
function macierz = stworzMacierz(m, n)
A=[];
for i=1:m+n
for j=1:m*n
A(i,j) = i; % just temporary something
end
end
macierz = A
end
Thank You in advance with help to that matter.
I will still try to find some solution, but maybe it is something easy to be done just I don't know how to look for it.

 採用された回答

Voss
Voss 2022 年 3 月 24 日

1 投票

m = 5;
n = 4;
Aeq = [repelem(eye(m),1,n); repmat(eye(n),1,m)]
Aeq = 9×20
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1

2 件のコメント

Grzegorz Szadkowski
Grzegorz Szadkowski 2022 年 3 月 24 日
Wow, thank You for quick answer.
I did not expect that it is possible to do it with one row of code.
On more time thank You.
Voss
Voss 2022 年 3 月 24 日
You're welcome! Matrix operations put the MAT in MATLAB. If this answer works, please mark it as 'Accepted'. Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 3 月 24 日

コメント済み:

2022 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by