Matrices and vectors

1 回表示 (過去 30 日間)
Nuno
Nuno 2011 年 10 月 28 日
I have a vector with 4 elements inside, like V=[1 2 3 4] and also have a 20x20 matrix. i want to multiply one value from V to the 10x10 sub-matrices from the original 20x20, Like this, first element in vector V will multiply by first submatrix (assuming that matrices are ordered left to right and top bottom) and second element in vector V by the second 10x10 sub-matrix and so on. So in the end, and for this example case, i will have a 20x20 matrix with the first 100 elements (first submatrix) with number 10, second one with 20, third with 30 and fourth with number 40. I'm having some difficulties in working this out, i've tryed this:
Vmatrix=ones(20);
f=[10 20 30 40];
d=mat2cell(Vmatrix,10*ones(1,2),10*ones(1,2));
for i=1:2
for j=1:2
for l=1:10
for k=1:10
for p=1:4
M(i,j)=d{i,j}(l,k)*f(:,p)
end
end
end
end
end
But it doesn't seem to work...

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 28 日
Vmatrix=ones(20);
f=[10 20 30 40];
d=mat2cell(Vmatrix,10*ones(1,2),10*ones(1,2));
for k=1:4
d{k}=d{k}*f(k);
end
NewV=cell2mat(d')
  1 件のコメント
Nuno
Nuno 2011 年 10 月 28 日
This worked! thanks

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

その他の回答 (3 件)

Darin McCoy
Darin McCoy 2011 年 10 月 28 日
How about something like this?
Vmatrix = ones(20)
f = [10 20 30 40]
Vmatrix(1:10,1:10) =Vmatrix(1:10,1:10) * f(1);
Vmatrix(11:20,1:10) = Vmatrix(11:20,1:10)* f(2);
Vmatrix(1:10,11:20) = Vmatrix(1:10,11:20)* f(3);
Vmatrix(11:20,11:20)=Vmatrix(11:20,11:20)* f(4);
  1 件のコメント
Nuno
Nuno 2011 年 10 月 28 日
yes, this is the most straight forward way, but isn't there a automatic way of doing this?

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


fjnb86
fjnb86 2011 年 10 月 28 日
I tried to understand you but I lost at the end of your explanation.
If you want to generate matrix try to avoid the use of loops.
MATLAB is exceptional for matrix operations and you should take a look of multiply element by element ".*" and subindex management
  2 件のコメント
Nuno
Nuno 2011 年 10 月 28 日
Imagine you have a 20x20 matrix, if you divide it in 4, you'll have four 10x10 matrices. i want to multiply each one of this matrices by one element in the vector V so that first element in vector is multiplied by first matrix and second element in vector by second matrix and so on...
fjnb86
fjnb86 2011 年 10 月 28 日
In this case Darin McCoy wrote you the answer ;)

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


Walter Roberson
Walter Roberson 2011 年 10 月 28 日
Your current code is equivalent to:
Vmatrix=ones(20);
f=[10 20 30 40];
d=mat2cell(Vmatrix,10*ones(1,2),10*ones(1,2));
for i=1:2
for j=1:2
l=10
k=10
p=4
M(i,j)=d{i,j}(l,k)*f(:,p)
end
end
Is that what you intended?
  1 件のコメント
Nuno
Nuno 2011 年 10 月 28 日
No, as i said in the previous comment i would like to multiply the elements from vector V by each one of sub matrix (10x10). first element in V by the first matrix and so on...

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by