Trying to stack an image array, error "Unable to perform assignment because the size of the left side is 220-by-220 and the size of the right side is 256-by-256"
1 次查看(过去 30 天)
显示 更早的评论
I recieved a code in a matlab file that went as follows:
input_dir = './data/';
imgs = dir([input_dir '*.png']);
for i = 1:length(imgs)
temp = imread([input_dir imgs(i).name]);
stack(:,:,i) = double(temp);
end
I get this error: "Index in position 3 exceeds array bounds. Index must not exceed 1."
I tried to simplify the code to see if I could limit the issues I had:
image_unknown = imread('.\Images\Greyscale\data_0001.png');
for i = 1:length(image_unknown)
stack(:,:,i) = double(image_unknown);
end
I got this error: "Unable to perform assignment because the size of the left side is 220-by-220 and the size of the right side is 256-by-256".
The image is 256X256 pixels, but I have no idea why it says 220X220. I tried reading about what the stack function does, but it wasn't clear to me. Could someone explain what is happening here?
0 个评论
采纳的回答
KSSV
2022-1-24
If you RHS image is 3D and if the dimensions of each image are different, then you cannot save them into a matrix. So save them into a cell as shown below.
input_dir = './data/';
imgs = dir([input_dir '*.png']);
N = length(imgs) ;
stack = cell(N,1) ;
for i = 1:length(imgs)
temp = imread([input_dir imgs(i).name]);
stack{i} = double(temp);
end
You can access the cell array using stack{1}. stack{2}...stack{n}.
2 个评论
KSSV
2022-1-24
编辑:KSSV
2022-1-24
That means you are trying to extract the elements which are not present in the array. So it seems, your stack is only a 2D matrix, not a 3D matrix.
EXample:
A = rand(2,2,5) ; % a 2x2x5 matrix for demo
A(:,:,1:2) ; % no error
A(:,:,2:6) ; % error as there is no 6th matrix, it has only 5 2D arrays
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!