merge multiple doubles in one cell of different sizes

1 回表示 (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by