@@ -942,11 +942,19 @@ Mat<eT>::init(const std::initializer_list< std::initializer_list<eT> >& list)
942942
943943 uword x_n_rows = uword (list.size ());
944944 uword x_n_cols = 0 ;
945+ uword x_n_elem = 0 ;
945946
946947 auto it = list.begin ();
947948 auto it_end = list.end ();
948949
949- for (; it != it_end; ++it) { x_n_cols = (std::max)(x_n_cols, uword ((*it).size ())); }
950+ for (; it != it_end; ++it)
951+ {
952+ const uword x_n_cols_new = uword ((*it).size ());
953+
954+ x_n_elem += x_n_cols_new;
955+
956+ x_n_cols = (std::max)(x_n_cols, x_n_cols_new);
957+ }
950958
951959 Mat<eT>& t = (*this );
952960
@@ -959,6 +967,9 @@ Mat<eT>::init(const std::initializer_list< std::initializer_list<eT> >& list)
959967 t.set_size (x_n_rows, x_n_cols);
960968 }
961969
970+ // if the inner lists have varying number of elements, treat missing elements as zeros
971+ if (t.n_elem != x_n_elem) { t.zeros (); }
972+
962973 uword row_num = 0 ;
963974
964975 auto row_it = list.begin ();
@@ -977,11 +988,6 @@ Mat<eT>::init(const std::initializer_list< std::initializer_list<eT> >& list)
977988 ++col_num;
978989 }
979990
980- for (uword c=col_num; c < x_n_cols; ++c)
981- {
982- t.at (row_num, c) = eT (0 );
983- }
984-
985991 ++row_num;
986992 }
987993 }
@@ -6796,21 +6802,10 @@ Mat<eT>::set_size(const uword new_n_elem)
67966802 {
67976803 arma_extra_debug_sigprint ();
67986804
6799- switch (vec_state)
6800- {
6801- case 0 :
6802- // fallthrough
6803- case 1 :
6804- init_warm (new_n_elem, 1 );
6805- break ;
6806-
6807- case 2 :
6808- init_warm (1 , new_n_elem);
6809- break ;
6810-
6811- default :
6812- ;
6813- }
6805+ const uword new_n_rows = (vec_state == 2 ) ? uword (1 ) : uword (new_n_elem);
6806+ const uword new_n_cols = (vec_state == 2 ) ? uword (new_n_elem) : uword (1 );
6807+
6808+ init_warm (new_n_rows, new_n_cols);
68146809
68156810 return *this ;
68166811 }
@@ -6854,23 +6849,10 @@ Mat<eT>::resize(const uword new_n_elem)
68546849 {
68556850 arma_extra_debug_sigprint ();
68566851
6857- switch (vec_state)
6858- {
6859- case 0 :
6860- // fallthrough
6861- case 1 :
6862- (*this ).resize (new_n_elem, 1 );
6863- break ;
6864-
6865- case 2 :
6866- (*this ).resize (1 , new_n_elem);
6867- break ;
6868-
6869- default :
6870- ;
6871- }
6852+ const uword new_n_rows = (vec_state == 2 ) ? uword (1 ) : uword (new_n_elem);
6853+ const uword new_n_cols = (vec_state == 2 ) ? uword (new_n_elem) : uword (1 );
68726854
6873- return *this ;
6855+ return ( *this ). resize (new_n_rows, new_n_cols) ;
68746856 }
68756857
68766858
0 commit comments