Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

concatenateMat.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function concatenatedMat = concatenateMat(mat1, mat2, method)
  2. % concatenatedMat = concatenateMat(mat1, mat2, method)
  3. %
  4. % Function concatenates two matrices of any dimensions by appending the
  5. % smaller matrix with trailing zeros or NaNs.
  6. % Input: mat1.
  7. % mat2.
  8. % method - concatenate either vertically ('vertical') or
  9. % horizontally ('horizontal'). This input is optional and
  10. % the default method is vertical. In case when trailing
  11. % NaNs are needed rather than zeros, corresponding methods
  12. % are 'verticalnan' and 'horizontalnan', respectively.
  13. % Output: concatenatedMat.
  14. if nargin < 3
  15. method = 'vertical';
  16. end
  17. if ~isempty(mat1) && ~isempty(mat2)
  18. if strcmp(method, 'vertical')
  19. diff = size(mat1,2) - size(mat2,2);
  20. if diff > 0
  21. trailingMat = zeros(size(mat2,1), abs(diff));
  22. mat2 = [mat2 trailingMat];
  23. elseif diff < 0
  24. trailingMat = zeros(size(mat1,1), abs(diff));
  25. mat1 = [mat1 trailingMat];
  26. end
  27. concatenatedMat = [mat1; mat2];
  28. elseif strcmp(method, 'horizontal')
  29. diff = size(mat1,1) - size(mat2,1);
  30. if diff > 0
  31. trailingMat = zeros(abs(diff), size(mat2,2));
  32. mat2 = [mat2; trailingMat];
  33. elseif diff < 0
  34. trailingMat = zeros(abs(diff), size(mat1,2));
  35. mat1 = [mat1; trailingMat];
  36. end
  37. concatenatedMat = [mat1 mat2];
  38. elseif strcmp(method, 'verticalnan')
  39. diff = size(mat1,2) - size(mat2,2);
  40. if diff > 0
  41. trailingMat = nan(size(mat2,1), abs(diff));
  42. mat2 = [mat2 trailingMat];
  43. elseif diff < 0
  44. trailingMat = nan(size(mat1,1), abs(diff));
  45. mat1 = [mat1 trailingMat];
  46. end
  47. concatenatedMat = [mat1; mat2];
  48. elseif strcmp(method, 'horizontalnan')
  49. diff = size(mat1,1) - size(mat2,1);
  50. if diff > 0
  51. trailingMat = nan(abs(diff), size(mat2,2));
  52. mat2 = [mat2; trailingMat];
  53. elseif diff < 0
  54. trailingMat = nan(abs(diff), size(mat1,2));
  55. mat1 = [mat1; trailingMat];
  56. end
  57. concatenatedMat = [mat1 mat2];
  58. end
  59. elseif ~isempty(mat1) && isempty(mat2)
  60. concatenatedMat = mat1;
  61. elseif isempty(mat1) && ~isempty(mat2)
  62. concatenatedMat = mat2;
  63. else
  64. concatenatedMat = [];
  65. end