Clear Filters
Clear Filters

merge multiple doubles in one cell of different sizes

4 views (last 30 days)
I created a cell using for loop:
clearvars;
for i=1:30
p = double(DSC00080);
X=4912; Y=3264;d=i;
r = p(:,:,1); % Red channel
g = p(:,:,2); % Green channel
b = p(:,:,3); % Blue channel
dr{i}= r(1:Y,1:X-d)+r(1:Y,1+d:X) ;
dg{i}= g(1:Y,1:X-d)+g(1:Y,1+d:X) ;
db{i}= b(1:Y,1:X-d)+b(1:Y,1+d:X) ;
end
it gives me a cell of 30 doubles with slight diiference in the size, as shown in the image.
Now I need to merge all 30 doubles in the cell into 1 double of 3264*4911. Could anyone tell me how to do it? many thanks.

Accepted Answer

Garmit Pant
Garmit Pant on 5 Aug 2024
Hello Wang Ryan
Given that the double arrays created in the cells have different sizes, you need to resize these arrays so that they all have the same dimensions before you can add them together.
The following code snippet will help you resize the arrays in the cells and then sum them:
% Initialize the resulting matrices
result_r = zeros(3264, 4911);
result_g = zeros(3264, 4911);
result_b = zeros(3264, 4911);
% Loop through each cell and merge them
for i = 1:30
% Get the current matrices
current_r = dr{i};
current_g = dg{i};
current_b = db{i};
% Determine the size of the current matrices
[rows, cols] = size(current_r);
% Trim or pad the matrices to ensure they are 3264x4911
if cols > 4911
current_r = current_r(:, 1:4911);
current_g = current_g(:, 1:4911);
current_b = current_b(:, 1:4911);
elseif cols < 4911
current_r(:, end+1:4911) = 0; % Pad with zeros
current_g(:, end+1:4911) = 0; % Pad with zeros
current_b(:, end+1:4911) = 0; % Pad with zeros
end
% Add the current matrices to the result
result_r = result_r + current_r;
result_g = result_g + current_g;
result_b = result_b + current_b;
end
I hope you find the above explanation and suggestions useful!
  2 Comments
Wang Ryan
Wang Ryan on 6 Aug 2024
Thank you very much, that's exactly what I need. Have a great day.
Garmit Pant
Garmit Pant on 6 Aug 2024
Glad that it helped you! Please accept the answer if this works for you. Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!