errors in using RESHAPE FUNCTION......PLEASE HELP ME.

4 views (last 30 days)
Error using reshape
To RESHAPE the number of elements must not change.
Error in load_database (line 10)
all_Images(:,(i-1)*5+j)=reshape(image_Container,size(image_Container,1)*size(image_Container,2),1);

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 6 Jun 2021
Note that while using reshape(), size of an array to be reshaped must be compatible with its being reshape. E.g.:
A = randi([0, 5], 5, 4);
B1 = reshape(A, 3, 3) % Does not work because of the size mismatch
B2 = reshape(A, 3, 4) % Does not work because of the size mismatch
B3 = reshape(A, 2, 10) % Works perfectly well!
Thus, check the size of your array in the position of [A] vs. sizes to be reshaped before proceeding reshape operation.

Steven Lord
Steven Lord on 6 Jun 2021
Edited: Steven Lord on 6 Jun 2021
More likely than not your image_Container variable has 3 or more dimensions.
x = randi(10, [2 3 4]);
sz = size(x)
sz = 1×3
2 3 4
sz1 = size(x, 1)
sz1 = 2
sz2 = size(x, 2)
sz2 = 3
y = reshape(x, sz1*sz2, 1)
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
You can't stuff 24 elements (since x is 2-by-3-by-4) into a 6-by-1 vector.
If you want to reshape the array into a column vector, two ways to do this are to use the colon index or to use reshape and use either [] (to make reshape calculate the size in that dimension) or numel.
y1 = x(:);
y2 = reshape(x, [], 1);
y3 = reshape(x, numel(x), 1);
whos x y1 y2 y3

Categories

Find more on Resizing and Reshaping Matrices 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!