substitute each element of a vector into a matrix without using loop

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.

 채택된 답변

Jan
Jan 2013년 11월 19일
Like this?
vector = 1:1:10;
vector = reshape( vector, [1, 1, numel(vector)] );
one_vector = ones( 1, 1, numel(vector) );
matrix = [4 * vector, 5 * one_vector; 4 * one_vector, 2 * vector ];

댓글 수: 5

Thank you that looks very good. But I don't understand what this line does
vector = reshape( vector, [1, 1, numel(vector)] );
can you explain it please?
It forces vector to have the dimension [1, 1, numel(vector)]. I.e. if you will, the result is a 3D matrix with exactly one row, one column and numel(vector) elements in the third dimension.
Another question, I want to find the determinant of each matrix. So when I try det(matrix) it doesn't work, but with det(matrix(:,:,10)) for example it does. Can I apply 'det' to all elements of 'matrix'?
Not as far as I can see. You will probably have to iterate over the third dimension.

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

추가 답변 (3개)

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

x is symbolic so my example should really be something like
matrix1=[4*x 5 ; 4 2*x];
Sean de Wolski
Sean de Wolski 2013년 11월 19일
편집: Sean de Wolski 2013년 11월 19일
i.e. the second half of what I have...
Yes but if I want the matrix for vector1(4) how do I display it?
Yeh but I want all of the matrices to be stored in an array so that I can call on them when I need them.

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

Jan
Jan 2013년 11월 19일
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

This is almost there! in my example I have each element multiplied by x, but this was probably misleading as each element will not be multiplied by x. So more like
matrix1 = [4*x 5 ; 4 2*x];

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

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by