creating a repeated matrix 2

Hi all,
I have
k1x=repmat(h(19,1), [3,1]) ;
k2x=repmat(h(37,1), [3,1]) ;
k3x=repmat(h(55,1), [3,1]) ;
k4x=repmat(h(73,1), [3,1]) ;
k5x=repmat(h(91,1), [3,1]) ;
kxtotal= [k1x; k2x;k3x;k4x;k5x];
h is a column of numnbers As you can see
37=19+18
55=37+18
73=55+18.
So I add the number 18 to obtain the sequence 19, 37, 55, 73 ...
Is there a better or clever way to construct kxtotal.
To be honest Isakson, I would prefer (as you did in my previous question) something like
ixs = repmat( [1:6], [3,1] );
ktotal = arrayfun( @(ii) sprintf('S%u',ii), ixs(:), 'uni', false );
that is, the approach without the loop.
thanks

7 件のコメント

Sabbas
Sabbas 2012 年 7 月 2 日
編集済み: Sabbas 2012 年 7 月 2 日
I think that the solution is
ktotal = repmat( [h(19:18:19+4*18,1)], [3,1] );
Am i right?
thanks
per isakson
per isakson 2012 年 7 月 2 日
編集済み: per isakson 2012 年 7 月 2 日
In my checklist for good code "Easy to read and understand" is one of the top items. I find the for-loop variant of my previous answer easier to understand.
If the performance of the for-loop is not good enough I search for another solution.
Sabbas
Sabbas 2012 年 7 月 2 日
thanks Isakson!
per isakson
per isakson 2012 年 7 月 2 日
"h(19,1)" that is not the 19:th value of a column vector?
Sabbas
Sabbas 2012 年 7 月 2 日
編集済み: Sabbas 2012 年 7 月 2 日
yes h(19,1)" is the 19:th value of a column vector.
I just realized that what I proposed is wrong
that is
ktotal = repmat( [h(19:18:19+4*18,1)], [3,1] );
is wrong
per isakson
per isakson 2012 年 7 月 2 日
How do I get the vector, h?
Sabbas
Sabbas 2012 年 7 月 2 日
Sorry h=rand(100,1)

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

 採用された回答

per isakson
per isakson 2012 年 7 月 2 日
編集済み: per isakson 2012 年 7 月 2 日

0 投票

Try this
ktotal = repmat( transpose( h([19:18:19+4*18]) ), [3,1] );
ktotal = ktotal(:);
I use "transpose" instead of the blip, "'", because the latter is too easy to miss.

1 件のコメント

Sabbas
Sabbas 2012 年 7 月 2 日
Many thanks! it is ok!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by