how can I loop a repmat function over columns of matrix?

3 ビュー (過去 30 日間)
shaimaa zidan
shaimaa zidan 2019 年 9 月 12 日
回答済み: dpb 2019 年 9 月 14 日
I have 3 matrices v1,v2,v3 each matrix is (31 row *10 column) and a column vector w (31* 1).
I used this function to get (r11) which equal the repmat vector of values in vector (w) by values in the first column of matrix (v1).
r11=[];
for i=1:length(w);
r11=[r11 repmat(w(i),1,v1(i,1))];
end
could you help me to loop this function over each column in matrix (v1) ,then looping over each column in matrices (v2),(v3)?
thank you.
  1 件のコメント
dpb
dpb 2019 年 9 月 12 日
What is the size and shape of the desired output?

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

回答 (1 件)

dpb
dpb 2019 年 9 月 14 日
Rereading, I guess it's just
r=cell2mat(arrayfun(@(v,w) repelem(v,w,1),v,repmat(w,1,3),'uni',0));
for each array v.
Naming variables with numeric subscripts makes difficult to write generic code; use a cell array or other structure over which can iterate programmatically instead. Or, the above solution would work if wrote
r=cell2mat(arrayfun(@(v,w) repelem(v,w,1),[v1 v2 v3],repmat(w,1,3),'uni',0));
Then the results would be in each of the three sets of 10 columns in the resulting array

カテゴリ

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