substitute each element of a vector into a matrix without using loop
Mostrar comentarios más antiguos
Hi I want to substitute each element of vector1 into 'x' in matrix1 and store each matrix in an array without using a loop. Please tell me how.
vector1=[1:1:10];
matrix1=[4*x 5*x ; 4*x 2*x];
Thanks in advance.
Respuesta aceptada
Más respuestas (3)
Sean de Wolski
el 19 de Nov. de 2013
So:
vector1=[1:1:10];
x = vector1;
matrix1=[4*x 5*x ; 4*x 2*x];
Or is x symbolic?
clear x;
syms x
matrix1=[4*x 5*x ; 4*x 2*x];
matrix1 = subs(matrix1,x,vector1)
5 comentarios
AllKindsofMath AllKinds
el 19 de Nov. de 2013
Sean de Wolski
el 19 de Nov. de 2013
Editada: Sean de Wolski
el 19 de Nov. de 2013
i.e. the second half of what I have...
AllKindsofMath AllKinds
el 19 de Nov. de 2013
Sean de Wolski
el 19 de Nov. de 2013
subs(matrix1,x,vector1(4))
AllKindsofMath AllKinds
el 19 de Nov. de 2013
Jan
el 19 de Nov. de 2013
Maybe something like the following?
matrix = [4, 5; 4, 2];
[p, q] = size( matrix );
vector = 1:1:10;
matrix = repmat( matrix(:), 1, numel( vector ) );
matrix = matrix .* repmat( vector, p*q, 1 );
matrix = reshape( matrix, p, q, numel( vector ) );
This gives you a 3d matrix, where each layer contains the specified matrix, mulitplied by one entry in vector
1 comentario
AllKindsofMath AllKinds
el 19 de Nov. de 2013
Alfonso Nieto-Castanon
el 20 de Nov. de 2013
perhaps something like:
f = @(x)[4*x 5 ; 4 2*x]; % Matrix in functional form
vector = 1:10; % Your vector of values for 'x'
matrix = arrayfun(f,vector,'uni',0); % A cell array of matrices
values = cellfun(@det,matrix); % Determinant of each of those matrices
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!