how to fill matrix without using for loop?

a=zeros(24,4); b=ones(24,1);
Why does not this work?
a(:,1:end)=b(:)
Subscripted assignment dimension mismatch.
Does anyone know where I am going wrong? bold

 採用された回答

Dale
Dale 2013 年 11 月 11 日
編集済み: Dale 2013 年 11 月 11 日

0 投票

Are you trying to replicate b through A? In which case use the repmat function:
A = repmat(b,24,1)
Edit: Just realised that b is a column, so it should be A = repmat(b,1,4)

その他の回答 (2 件)

Wayne King
Wayne King 2013 年 11 月 11 日
編集済み: Wayne King 2013 年 11 月 11 日

0 投票

because b isn't the right size. b is 24x1 but you are trying to fill all 4 columns of a
How about just
a = ones(24,4);
or if you really want to "fill" starting with zeros.
a = zeros(24,4);
b = ones(24,1);
b = repmat(b,1,4);
a = b;
Carlos Oliveira
Carlos Oliveira 2013 年 11 月 11 日

0 投票

Thank you Dale. :)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2013 年 11 月 11 日

回答済み:

2013 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by