appending zeros to multi-array matrix
Show older comments
I have a 283x283xM matrix where M may vary in size. I want the value of M to be equal to the length of a variable (let's say 'filenames'. The problem is that M will not always equal the length of 'filenames' (i.e., M = 273, but length(filenames) = 275). filenames will always be an M x 1 matrix such that M = 273, 274, 275, or 276.
I want to append zeros to my 283x283xM matrix such that the length would be equal to the length of 'filenames'. Most of the answers on here are for single array matrices, and am having difficulties getting this to work out. Any help would be appreciated!
Answers (2)
KSSV
on 17 May 2016
k = rand(3,3,2) ; % Random matrix of order 3x3x3
% now I want to make k to 3x3x3 adding zeros
k(:,:,3) = zeros(size(k(:,:,1)))
but by adding zeros you are wasting memory...
The simplest way is to simply put a zero as the last element, in the bottom corner of your 3D matrix. The necessary pages full of zero will automatically be added:
%demo data
somemat = rand(283, 283, 273);
filenames = cell(275, 1);
somemat(end, end, numel(filenames)) = 0; %prefer numel to length. It's more reliable
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!