merge multiple doubles in one cell of different sizes

4 次查看(过去 30 天)
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.

采纳的回答

Garmit Pant
Garmit Pant 2024-8-5
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 个评论
Wang Ryan
Wang Ryan 2024-8-6
Thank you very much, that's exactly what I need. Have a great day.
Garmit Pant
Garmit Pant 2024-8-6
Glad that it helped you! Please accept the answer if this works for you. Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by