appending zeros to multi-array matrix
显示 更早的评论
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!
回答(2 个)
KSSV
2016-5-17
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
类别
在 帮助中心 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!