Pre-Allocate Space for a Cell Array
1 回表示 (過去 30 日間)
古いコメントを表示
I'm working with confocal microscopy images. Specifically Z-stacks. I want to manipulate the images. However, I'm having a problem preallocating the space in memory.
%Open Image
bfopen()
%Extract Image Data
image_data=ans{1,1};
%Create an Array focused on the pixel_data
pixel_data=image_data(:,1);
%Ask User How Many Laser Channels were used to Acquire the Image
total_laser_channels=input('How many laser channels were used to acquire this image?\n');
%Determine the number of image slices in each channel
images_per_channel=length(pixel_data)/total_laser_channels;
*
%Preallocate space and variables for each channel
if total_laser_channels==1
channel1{images_per_channel,:}=[];
elseif total_laser_channels==2
channel1{images_per_channel,:}=[];
channel2{images_per_channel,:}=[];
elseif total_laser_channels==3
channel1{images_per_channel,:}=[];
channel2{images_per_channel,:}=[];
channel3{images_per_channel,:}=[];
elseif total_laser_channels==4
channel1{images_per_channel,:}=[];
channel2{images_per_channel,:}=[];
channel3{images_per_channel,:}=[];
channel4{images_per_channel,:}=[];
elseif total_laser_channels==5
channel1{images_per_channel,:}=[];
channel2{images_per_channel,:}=[];
channel3{images_per_channel,:}=[];
channel4{images_per_channel,:}=[];
channel5{images_per_channel,:}=[];
else
break
end*
%Loop: Each laser channel has corresponding images
i=1;
while i<length(pixel_data)+1
if total_laser_channels==1
channel1=[channel1;pixel_data(i,1)];
i=i+1;
elseif total_laser_channels==2
channel1=[channel1;pixel_data(i,1)];
channel2=[channel2;pixel_data(i+1,1)];
i=i+2;
elseif total_laser_channels==3
channel1=[channel1;pixel_data(i,1)];
channel2=[channel2;pixel_data(i+1,1)];
channel3=[channel3;pixel_data(i+2,1)];
i=i+3;
elseif total_laser_channels==4
channel1=[channel1;pixel_data(i,1)];
channel2=[channel2;pixel_data(i+1,1)];
channel3=[channel3;pixel_data(i+2,1)];
channel4=[channel4;pixel_data(i+3,1)];
i=i+4;
elseif total_laser_channels==5
channel1=[channel1;pixel_data(i,1)];
channel2=[channel2;pixel_data(i+1,1)];
channel3=[channel3;pixel_data(i+2,1)];
channel4=[channel4;pixel_data(i+3,1)];
channel5=[channel5;pixel_data(i+4,1)];
i=i+5;
else
display('Too many or wrong number of laser channels.')
break
end
end
For the images i'm looking at now the total_laser_channels is 3. images_per_channel is 45. However for channel1, channel2, channel3 after i run the code i get a 46x1 array instead of the 45x1 array that i want.
[]
2048x2048 uint16
2048x2048 uint16
2048x2048 uint16
2048x2048 uint16
2048x2048 uint16
2048x2048 uint16
2048x2048 uint16....
The first position of the cell array has []
Any help would be greatly appreciated.
1 件のコメント
Ben11
2014 年 8 月 1 日
Maybe you could use a switch/case and a for loop? On a side note if you want to remove the empty array from your cell array you can use ths:
channel1 = channel1(~cellfun('isempty', channel1));
回答 (1 件)
Prateekshya
2024 年 10 月 3 日
Hello Jessica,
This error is occuring because of empty array initialization. Instead of initializing the cell arrays with empty arrays, try preallocating them with the correct size and directly assign some data. Here is an example how to do it:
channel1 = cell(images_per_channel, 1);
This will avoid unnecessary allocation and resizing of the arrays.
I hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!