merge multiple doubles in one cell of different sizes
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Wang Ryan
el 5 de Ag. de 2024
Comentada: Garmit Pant
el 6 de Ag. de 2024
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 comentario
Respuesta aceptada
Garmit Pant
el 5 de Ag. de 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 comentarios
Garmit Pant
el 6 de Ag. de 2024
Glad that it helped you! Please accept the answer if this works for you. Thanks!
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!