Effacer les filtres
Effacer les filtres

HELP copy matrix and inserting in another matrix

1 vue (au cours des 30 derniers jours)
Jah-lahfui
Jah-lahfui le 2 Jan 2015
Modifié(e) : dpb le 2 Jan 2015
Hello guys, i have a matrix
U=(416,411)
and i need to make a matrix
W=zeros(416+2,411+2)
and then i need to copy the matrix U inside the matrix W, so the borders of this new matrix become full of zeros, or in anothers words matrix U surrended by zeros.
For example:
[0 0 0 0 ...
0 1 1 0 ...
0 0 0 0]
´
thanks in advance

Réponse acceptée

dpb
dpb le 2 Jan 2015
W(2:end-1,2:end-1)=U;
using the fact size(W you already created is size(U)+2 and you wanted it centered. If it weren't centered, then the generic expression is written based on the size of U instead of W...
r1=2; c1=2 % row column starting points in target
[nr,nc]=size(U);
r2=r1+nr-1: c2=c1+nc-1; % end positions in W
W(r1:r2,c1:c2)=U;
The rule is the size() of the LHS addressing expression has to match that of the RHS.
  1 commentaire
Jah-lahfui
Jah-lahfui le 2 Jan 2015
yeah thanks a lot! You just made me felt dumb ehehe thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 2 Jan 2015
[n,m]=size(U)
W(1:n,1:m)=U
  1 commentaire
dpb
dpb le 2 Jan 2015
Modifié(e) : dpb le 2 Jan 2015
Just as a minor correction, the above places it in upper RH corner, Azzi...need correction for the offset to get it at the desired starting location.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and 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