Borrar filtros
Borrar filtros

Creating vector and matrix from vector

2 visualizaciones (últimos 30 días)
danielle sisserman
danielle sisserman el 10 de Dic. de 2020
Editada: James Tursa el 10 de Dic. de 2020
function w = svm(Xtrain, Ytrain)
first, I want to create the following vector:
data = [Xtrain(1)*Ytrain(1) Xtrain(2)*Ytrain(2) ... Xtrain(m)*Ytrain(m)]
Then I want to use vector 'data' to create the following matrix:
A = [data(1) 0 0 ... 0
data(2) 0 0 ... 0
.
.
.
data(m) 0 0 ... .]
How can I easily and efficiantly create the vector data and the matrix A?
Thank you.

Respuesta aceptada

James Tursa
James Tursa el 10 de Dic. de 2020
Editada: James Tursa el 10 de Dic. de 2020
Element-wise multiplication is done with the .* operator (with the dot). E.g.,
data = Xtrain .* Ytrain;
The operation data(:) will result in a column vector.
The zeros(rows,columns) will result in a 0's matrix of the requested size.
Then use the square brackets [ ] to concatenate them into a single matrix.
result = [data(:) zeros(rows,columns)];

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by