フィルターのクリア

How to multiply a variable for matrix?

1 回表示 (過去 30 日間)
Gabriel Oliviero
Gabriel Oliviero 2016 年 1 月 15 日
コメント済み: Walter Roberson 2016 年 1 月 15 日
Hello I'm trying to do this kind of operation:
A = 20:100:20000
B = [2 3 0 0 0 0; 3 4 -5 0 0 0; 0 7 8 -3 0 0; 0 0 3 2 -3 0; 0 0 0 2 1 1; 0 0 0 0 6 4]
C = A.*B
and it gives me the following error:
??? Error using ==> times Matrix dimensions must agree.
How can I fix it?

回答 (2 件)

Shane Overington
Shane Overington 2016 年 1 月 15 日
Hi Gabriel,
The issue here (as the returned error is indicating) is that the matrices you are trying to multiply are not the same size.
Matrix A is 1 by 200 and Matrix B is 6 by 6.
Are these the matrices you want to multiply, or should they be structured differently?
If you want to do an element-wise multiplication (i.e. C = A.*B) then A and B both need to be 1 by 200 matrices (or both 6 by 6), so that the request can be evaluated as a multiplication of each of the locations in the respective matrices.
For example:
A = [2 3 4 5 6 7];
B = [3 5 6 7 8 9];
C = A.*B;
Result is:
C = [6 15 24 35 48 63]
Regards, Shane
  2 件のコメント
Gabriel Oliviero
Gabriel Oliviero 2016 年 1 月 15 日
Actually what I'm trying to do is exactly multiply each element of A by the 6x6 matrix in order to plot a AxB graph.
Stephen23
Stephen23 2016 年 1 月 15 日
編集済み: Stephen23 2016 年 1 月 15 日
What size do you expect the output to be, when you multiply a 1x200 matrix with a 6x6 matrix? Which of these do you want?:

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


Walter Roberson
Walter Roberson 2016 年 1 月 15 日
If you want the entire matrix B to be multiplied by each element of A in turn, then
C = bsxfun(@times, reshape(A,1,1,[]), B);
This would produce a 6 x 6 x 200 array
  4 件のコメント
Gabriel Oliviero
Gabriel Oliviero 2016 年 1 月 15 日
My graph is the variation of frequencies (represented by A) versus the result of operations with 6x6 matrix from mass spring damper system. I want to relate the displacement of that system with the variation of frequencies.
Walter Roberson
Walter Roberson 2016 年 1 月 15 日
That does not tell me anything about what you want the graph to look like. Are you graphing surfaces? 3D lines? 36 2D lines across the width? 6 2D lines for each A value? 6 3D lines for each A value? A 6 x 6 contour plot for each A value?

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

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by