replace sub-matrix values with zeros

2 views (last 30 days)
Asim Biswas
Asim Biswas on 18 Aug 2011
Answered: BhaTTa on 8 Aug 2024
Hello, I have a large matrix C, which contains a number of matrices in the format C{i}{j}(k1,k2). The size of the matrix (value of k1 and k2) are different for different i and j values. I would like to set all k1 and k2 values to zero except for a certain i and j values. For example, i=1:7, and j varies from 1 to 128 for different i values. I want to keep the values of i=1, and j=1 and replace all other values to zero. e.g. C{1,1}{1,1}. Please help me to make the loop where i can keep the original size of the matrix but replace the values with zero except the target ones. Related information: I encountered this problem when I am using curvelet. I decomposed the image into different scales and wedges. Now I want to do an inverse curvelet transform of the original image without a particular scale and wedge. I would like to separate and visualize the variations at different scales and wedges. I wonder how I can make the matrix right. thanks a lot in advance. please help

Answers (1)

BhaTTa
BhaTTa on 8 Aug 2024
To achieve this, you need to loop through all elements of the cell array C and set the values to zero for all indices except for the specified ones (i=1 and j=1). You want to retain the original sizes of the matrices but replace their values with zeros.
Here's a MATLAB script to do this:
% Example setup: create a sample cell array C with nested cell arrays
C = cell(3, 3);
for i = 1:3
for j = 1:3
C{i, j} = cell(1, 3);
for k = 1:3
C{i, j}{k} = rand(i + 1, j + 1); % Random matrices of different sizes
end
end
end
% Display original C
disp('Original C:');
disp(C);
% Apply the zeroing operation
for i = 1:size(C, 1)
for j = 1:size(C, 2)
% Skip the specified indices (i=1 and j=1)
if ~(i == 1 && j == 1)
% Loop through each nested cell array
for k = 1:length(C{i, j})
% Get the size of the current matrix
[k1, k2] = size(C{i, j}{k});
% Set all elements to zero
C{i, j}{k} = zeros(k1, k2);
end
end
end
end
% Display modified C
disp('Modified C:');
disp(C);

Categories

Find more on Resizing and Reshaping Matrices 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!