Multiply each column of cell array by column vector

1 回表示 (過去 30 日間)
Carmel Howe
Carmel Howe 2015 年 10 月 26 日
コメント済み: Carmel Howe 2015 年 10 月 26 日
I have a 10x1 cell of separate events with each array having a size 384(time) x 5328(pixel). I want to multiply each column in each cell array by a column vector (C = 384x1). I have tried this code but it's not giving me the correct answer.
A = cell(size(B));
for jj = 1:10
A{jj,1} = B{jj,1},[],1 .* C;
end
Thanks

採用された回答

Stephen23
Stephen23 2015 年 10 月 26 日
編集済み: Stephen23 2015 年 10 月 26 日
As per your previous question you would actually be better off keeping your data in a simple numeric array rather than a cell array, so the first thing we will do is to convert it to a numeric array. The solution is then trivial using bsxfun:
>> X = {[1,2,3;4,5,6],[0,2,4;6,8,10]};
>> Y = cat(3,X{:})
Y(:,:,1) =
1 2 3
4 5 6
Y(:,:,2) =
0 2 4
6 8 10
>> C = [2;5]
C =
2
5
>> bsxfun(@times,C,Y)
ans(:,:,1) =
2 4 6
20 25 30
ans(:,:,2) =
0 4 8
30 40 50
This uses efficient vectorized code:

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 10 月 26 日
A = cell(size(B));
for jj = 1:10
A{jj} = bsxfun(@times,B{jj},C);
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by