Adding Matrices into main diagonal of Matrix

조회 수: 1 (최근 30일)
Thomas Rodriguez
Thomas Rodriguez 2023년 3월 21일
댓글: Stephen23 2023년 3월 28일
I'm trying to add main diagonal matrices into a defined matrix such as:
clc; clear; close all;
n = 10;
x = linspace(0,1,n);
F = @(x) [x.^2, sin(x), cos(x);
1, 0, 1;
x, cos(x), sin(x)];
A_mat = zeros(n,n);
for i = 1:n
tmp = F(x(i));
A = blkdiag(tmp);
end
A
A = 3×3
1.0000 0.8415 0.5403 1.0000 0 1.0000 1.0000 0.5403 0.8415
As we can see, matrix is not a 10x10 matrix with the main diagonal filled with the intended values above. How can I adjust this code to do such?

채택된 답변

Stephen23
Stephen23 2023년 3월 21일
편집: Stephen23 2023년 3월 21일
"As we can see, matrix is not a 10x10 matrix with the main diagonal filled with the intended values above."
After calling BLKDIAG with 10 3x3 matrices I would expect a 30x30 matrix as the output.
"How can I adjust this code to do such?"
A simpler, much more robust alternative (which also works for any sized input matrices):
N = 10;
F = @(x) [x.^2,sin(x),cos(x);1,0,1;x,cos(x),sin(x)];
C = arrayfun(F,linspace(0,1,N),'uni',0);
M = blkdiag(C{:})
M = 30×30
0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0123 0.1109 0.9938 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1111 0.9938 0.1109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0494 0.2204 0.9754 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2222 0.9754 0.2204 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1111 0.3272 0.9450 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  댓글 수: 4
Thomas Rodriguez
Thomas Rodriguez 2023년 3월 27일
@Stephen23 From your defined "container array", I tried to conduct "numerical manipulation"
N = 10;
F = @(x) [x.^2,sin(x),cos(x);1,0,1;x,cos(x),sin(x)];
C = cell(10,10);
C(diag(true(1,10))) = arrayfun(F,linspace(0,1,N),'uni',0)
C = 10×10 cell array
{3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {3×3 double}
pi.*C + C
Operator '.*' is not supported for operands of type 'cell'.
With this array, I plan to use some matrix manipulations like above. So, is their a particular way of doing manipulation with this array type? Or is it not possible to do so?
Stephen23
Stephen23 2023년 3월 28일
"So, is their a particular way of doing manipulation with this array type?"
You can loop over the elements (cells) of a cell array, or you can apply any function that is defined to operate on cell arrays. Perhaps CELLFUN helps you:
C = cellfun(@(m) pi.*m+m, C, 'uni',0)
"Or is it not possible to do so?"
Numeric operations (e.g. arithmetic) are defined for numeric arrays, not for container classes.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cameron
Cameron 2023년 3월 21일
Why would your matrix be 10x10 instead of 30x30? This is how I would do it if you haven't already calculated your tmp values ahead of time. I also made the code a bit more robust so you can add different size matrices instead of just 3x3 or any other square matrix.
clc; clear; close all;
n = 10;
x = linspace(0,1,n);
F = @(x) [x.^2, sin(x), cos(x);
1, 0, 1;
x, cos(x), sin(x)];
A_mat = zeros(n*size(F(x(1)),1),n*size(F(x(1)),2));
for i = 1:n
tmp = F(x(i));
rowindx = size(tmp,1)*i-(size(tmp,1)-1):size(tmp,1)*i;
colindx = size(tmp,2)*i-(size(tmp,2)-1):size(tmp,2)*i;
A(rowindx,colindx) = tmp;
end
disp(A)
Columns 1 through 19 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0123 0.1109 0.9938 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1111 0.9938 0.1109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0494 0.2204 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6184 0.7859 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0.7859 0.6184 0 0 0 0 0 0 0 0 0 0 0 0.6049 0.7017 0.7125 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0.7778 0.7125 0.7017 0 0 0 0 0 0 0 0 0 0 0 0.7901 0.7764 0.6303 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 0.8889 0.6303 0.7764 0 0 0 0 0 0 0 0 0 0 0 1.0000 0.8415 0.5403 0 0 0 0 0 0 0 0 1.0000 0 1.0000 0 0 0 0 0 0 0 0 1.0000 0.5403 0.8415

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by