Effacer les filtres
Effacer les filtres

How to assign parts of a matrix equal to a single vector

23 vues (au cours des 30 derniers jours)
David
David le 22 Avr 2012
In matlab if you had a vector called y = ones(5,5), you could do the following assignments:
y(:,1) = 0; First column in all rows equals zero.
y(:,1:2) = 0; First two columns in all rows equals zero.
y(1,:) = 0; First row equals zero, etc.
But what if you wanted to be more specific, say for example I had a vector x = [0 2 0] and I wanted y(2:4,2:4) = x; Meaning the middle 3 columns and middle 3 rows would be set to that vector. The problem is it doesn't accept this kind of assignment and gives a "Subscripted assignment dimension mismatch" error. I was wondering if there is any way to do this, or this something like this only possible through a for loop?

Réponse acceptée

Richard Brown
Richard Brown le 22 Avr 2012
It's because y(2:4, 2:4) is a 3x3 matrix, and so you must assign it a 3x3 matrix. The command repmat is an easy way to stack multiple copies of a matrix together. Assuming you want each of the rows of that 3x3 block to be [0 2 0]:
x = [0 2 0];
y(2:4, 2:4) = repmat(x, 3, 1);

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by