How can I add a column vector to every column within a cell array?

Ok, so I now have a cell array with 1000 m*n matrices which I have fiddled with earlier.
What I now need to do is to each column within each of those m*n matrices, add a column vector 'A'
So, my m*n matrices look like:
1 2 3, 4 5 6, 7 8 9,
my column vector is
1, 2, 1,
so i want my output to be
2 3 4, 6 7 8, 8 9 10,
Again, please keep this nice and simple for me :)

 採用された回答

Matt Fig
Matt Fig 2012 年 8 月 16 日
編集済み: Matt Fig 2012 年 8 月 16 日

0 投票

T = {magic(3); ones(3); zeros(3)}; % Initial cell array.
A = [1 2 3]; % The vector to add.
Tnew = cellfun(@(x) bsxfun(@plus,x,A'),T,'Un',0 ); % or 'T ='
Now compare:
T{1},Tnew{1}
BSXFUN is a great function to get to know if you are going to be spending time with MATLAB. And since you are working with cell arrays, you should learn CELLFUN too. It is like a FOR loop in one line!

3 件のコメント

John
John 2012 年 8 月 16 日
Perfect, thanks...
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 16 日
he asked to make it simple, i don't think you do
vinjamoori vikas
vinjamoori vikas 2020 年 12 月 17 日
Sir, I have total six matrices each with the size 4x4. I want to add all the combinations of the columns.
That means each column of one matrix with each other column of other matrices. I should get total of 4^6 summed vectors. Please suggest me something.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 16 日
編集済み: Azzi Abdelmalek 2012 年 8 月 16 日

0 投票

A=[1 2 3; 4 5 6; 7 8 9]
b=[1; 2; 1];
[n,m]=size(A);A=A+repmat(b',1,m)
%now for all your 1000 matrix, if they are in a cell array M
for k=1:1000
A=M{k}
% add a previous code
b=[1; 2; 1];
[n,m]=size(A);A=A+repmat(b',1,m)
end

2 件のコメント

John
John 2012 年 8 月 16 日
編集済み: John 2012 年 8 月 16 日
Ok, I'm having trouble - please bear with me, this is my first time on matlab!!!
I have the following code:
for k=1:1000
A=T{k}
[n,m]=size(A);A=A+repmat(a',1,m)
end
Where T is the cell array with the 1000 matrices inside and a is my column vector. Can you tell me where I'm going wrong...?
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 16 日
編集済み: Azzi Abdelmalek 2012 年 8 月 16 日
it's ok, maby you are missing a vector a. can you post error message? or part of your cell array and your vector.
another thing; when you tape T{1} what did you get?

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2012 年 8 月 16 日

コメント済み:

2020 年 12 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by