How to pad NaNs in a matrix having small size to make equivalent with matrix of large size?

40 vues (au cours des 30 derniers jours)
Hi there,
Suppose I have given matrices A= [1 2 3; 4 5 6; 7 8 9] and B=[1 2; 3 4]. Now, I just wanted to pad NaNs in matrix B to make equal size to that of A. Your help will be greatly appreciated.
Thank you in advance.

Réponse acceptée

Voss
Voss le 12 Juin 2022
A = [1 2 3; 4 5 6; 7 8 9];
B = [1 2; 3 4];
[ma,na] = size(A);
[mb,nb] = size(B);
% one way:
B_new = [B NaN(mb,na-nb); NaN(ma-mb,na)]
B_new = 3×3
1 2 NaN 3 4 NaN NaN NaN NaN
% another way:
B_new = NaN(ma,na);
B_new(1:mb,1:nb) = B
B_new = 3×3
1 2 NaN 3 4 NaN NaN NaN NaN
  2 commentaires
Sushil Pokharel
Sushil Pokharel le 12 Juin 2022
@Voss, thank you so much for your help. This is exactly what I wanted; I really appreciate your time and help.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 12 Juin 2022
A = [1 2 3; 4 5 6; 7 8 9];
B = [1 2; 3 4];
C = padarray(B, max(0, size(A) - size(B)), NaN, 'post')
C = 3×3
1 2 NaN 3 4 NaN NaN NaN NaN

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by