how to append matrix in a loop

2 ビュー (過去 30 日間)
Akriti Raj
Akriti Raj 2021 年 2 月 14 日
編集済み: Stephen23 2021 年 2 月 14 日
I have to write a code where: S1 is a random array with binary numbers, s2 is a random array with numbers in the range 1 to 10. And s3 is such that if s1= 1 0 1 1 0 and s2 = 2 4 3 1 2 then for s3 first 1 will be repeated 2 times, then 0 will be repeated 4 times and so on final output should be 1 1 0 0 0 0 1 1 1 1 0 0
my code:
N = input("Enter the length of random number: ")
S1 = randi([0 1], 1, N)
S2 = randi([1 10], 1, N)
for i=1
while i<=N
a=S1(1,i);
b=S2(1,i);
c=repelem(a,b);
c=transpose(c)
i=i+1;
endwhile
endfor
output: How to display all these c matrix as one , how to append them ?

回答 (1 件)

Stephen23
Stephen23 2021 年 2 月 14 日
編集済み: Stephen23 2021 年 2 月 14 日
s1 = [1,0,1,1,0];
s2 = [2,4,3,1,2];
v = repelem(s1,s2)
v = 1×12
1 1 0 0 0 0 1 1 1 1 0 0

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by