Borrar filtros
Borrar filtros

Adding Matrices into main diagonal of Matrix

3 visualizaciones (últimos 30 días)
Thomas Rodriguez
Thomas Rodriguez el 21 de Mzo. de 2023
Comentada: Stephen23 el 28 de Mzo. de 2023
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?

Respuesta aceptada

Stephen23
Stephen23 el 21 de Mzo. de 2023
Editada: Stephen23 el 21 de Mzo. de 2023
"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 comentarios
Thomas Rodriguez
Thomas Rodriguez el 27 de Mzo. de 2023
@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 el 28 de Mzo. de 2023
"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.

Iniciar sesión para comentar.

Más respuestas (1)

Cameron
Cameron el 21 de Mzo. de 2023
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 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 0 0 0 0 0 0 0 0 0 Columns 20 through 30 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 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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by