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 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
KSSV
on 24 Jan 2022
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 Comments
KSSV
on 24 Jan 2022
Edited: KSSV
on 24 Jan 2022
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
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!