merge multiple doubles in one cell of different sizes

조회 수: 2 (최근 30일)
Wang Ryan
Wang Ryan 2024년 8월 5일
댓글: Garmit Pant 2024년 8월 6일
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.
  댓글 수: 1
KSSV
KSSV 2024년 8월 5일
What exactly you are trying to do? What is DSC00080?

댓글을 달려면 로그인하십시오.

채택된 답변

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