フィルターのクリア

Expand vector along third dimension

28 ビュー (過去 30 日間)
Henrik Dam
Henrik Dam 2014 年 11 月 25 日
編集済み: Matt J 2014 年 11 月 29 日
My question is simply, but I cannot figure out how to do it.
Assume I have a vector K=[K_1,..,K_n] then I would like to create a three dimensional matrix M with first dimension equal to x, second dimension equal to y and third dimension equal to n such that M(:,:,1)=K1*ones(x,y), M(:,:,2)=K2 * ones(x,y) etc. I know of course I can loop, but I am expecting a neat one-liner exists.

採用された回答

Matt J
Matt J 2014 年 11 月 25 日
編集済み: Matt J 2014 年 11 月 25 日
I have a matrix S which is x times y. Then I would subtract each element of K (what I wanted to use the extra dimension for), take the positive part (i.e. max of the result and 0) and sum out the first dimension x to get a y times n matrix.
K=reshape(K,1,1);
result = squeeze( sum( max( bsxfun(@minus,S,K), 0), 1 ) )
  1 件のコメント
Matt J
Matt J 2014 年 11 月 29 日
編集済み: Matt J 2014 年 11 月 29 日
Henrik's comment moved from an "Answer" to here:
That is one sexy line of code :D Thanks!

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

その他の回答 (1 件)

Matt J
Matt J 2014 年 11 月 25 日
編集済み: Matt J 2014 年 11 月 25 日
but I am expecting a neat one-liner exists.
It does
M=repmat( reshape(K,1,1,[]), x, y);
but it is often better to use BSXFUN when applicable. That way you don't have to allocate any actual memory for duplicate data.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by