Error using reshape to reshape the number of elements must not change

This code was written by Stephen: a = [0.75 0.23 -2.86 0.75 0.47 3.87 0.75 0.64 2.78 2.45 0.23 4.56 2.45 0.47 0.89 2.45 0.64 2.74 3.23 0.23 2.10 3.23 0.47 -3.65 3.23 0.64 ...

Ethan Gros

This code was written by Stephen:

surf(x,y,z, ‘FaceColor’,‘interp’)

The code above works, but when I added four more point (each row is a point) the reshape gets an error saying:

«Error using reshape

To RESHAPE the number of elements must not change.

Error in Code_Version_1 (line 16)

x = reshape(a(:,1),3,3); «

Code below gets the error.

surf(x,y,z, ‘FaceColor’,‘interp’)

I believe it is because the dimensions of the matrix is changing(from 9×3 to 13×3), but I do not know how to alter the code in order to accommodate for the change in the new dimensions of the matrix.

Accepted Answer

dpb

Edited: dpb

on 9 Jun 2015

Well, since 13 is prime, you’re stuck, there is no factor of 13 other than 1 and itself.

By symmetry to the initial, it looks like you need multiples of three and I note that for the initial data the first column value is repeated that many times—altho I’ve not tried to dig into what Stephen was doing here, I’d venture that is probably an unstated assumption for the method to work.

But, the general syntax with a proper set of input data (namely length divisible by N) would be

where the [] tells Matlab to use length(x)/N as the second dimension.

Now whether that works for the intended purpose here is another question altho I would venture it would if the additional points follow the same pattern as the initial data set.


More Answers (0)

See Also

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

function ImageSharing

clear; close all;

Height = 256;

Width =256;

t = 4;

w = 8;

Users = [1,3,5,8];

base = 257;

Sub_width = Width/t^(0.5);

Sub_height = Height/t^(0.5);

if(length(Users)<t)

error(‘invalid length of Users’);

end;

if(mod(Sub_height,1) ~= 0)

error(‘invalid length of Sub height’);

end;

Im_Name = ‘i.bin’;

[Im_linear, Im_Square] = ReadImage(Im_Name,Height,Width);

P_Im = GetPreparedIm(Im_linear,t);

Shadow_Image = GetShadow_Image(P_Im,w,base);

a = find(Shadow_Image>256);

ShowShadowImage(Shadow_Image,Sub_width,Sub_height);

RequiredShadowImage = GetRequiredShadowImage(Shadow_Image,Users);

RecoveredImage = RecoverImage(RequiredShadowImage,Users,Height,Width,base);

Err = sum(RecoveredImage(:) — Im_linear);

disp(sprintf(‘Error bit: %d’,Err));

figure;

imshow(uint8(RecoveredImage));

title(‘Recovered Image’);

function RequiredShadowImage = AddNoise(RequiredShadowImage)

TT = size(RequiredShadowImage);

Err = round(rand(TT(1),TT(2)));

Err(Err == 0) = -1;

RequiredShadowImage = RequiredShadowImage + int32(Err);

function RecoveredImage = RecoverImage(RequiredShadowImage,Users,Height,Width,base)

TT = size(RequiredShadowImage);

xx = ones(TT(2),TT(2));

for i = 1:TT(2)

xx(:,i) = xx(:,i).*(Users.^(i-1))’;

end;

for i = 1:TT(1)

if(mod(i,100)==0)

disp(sprintf(‘%d of %d’,i/100,floor(TT(1)/100)));

end;

RecoveredImage(i,:) = solveEq(xx,RequiredShadowImage(i,:),base);

end;

RecoveredImage = reshape(RecoveredImage(:),Height,Width)’;

function b = solveEq(xx,yy,base)

b = GetIntMod(inv(sym(xx))*yy’,base)’;

function Re_b = GetIntMod(b,base)

[n,d] = numden(b);

n = double(n);

d = double(d);

Re_b = mod(n.*powermod(d,-1,base),base);

function RequiredShadowImage = GetRequiredShadowImage(Shadow_Image,Users)

TT = length(Users);

for i = 1:TT

RequiredShadowImage(:,i) = Shadow_Image(:,Users(i));

end;

function ShowShadowImage(Shadow_Image,Sub_width,Sub_height)

TT = size(Shadow_Image);

for i =1:TT(2)

figure;

imshow(uint8(reshape(Shadow_Image(:,i),Sub_width,Sub_height)));

cmd = sprintf(‘title(»Shadow Image %d»)’,i);

eval(cmd);

end;

function Shadow_Image = GetShadow_Image(P_Im,w,base)

TT = int32(size(P_Im));

P_Im = int32(P_Im);

w=int32(w);

base = int32(base);

Shadow_Image = int32(zeros(TT(1),w));

for i=1:w

for j = 1:TT(2)

Shadow_Image(:,i) = Shadow_Image(:,i) + mod(P_Im(:,j)*i^(j-1), base);

end;

end;

Shadow_Image = mod(Shadow_Image,base);

function P_Im = GetPreparedIm(Im_linear,t)

if(mod(length(Im_linear),t) ~= 0)

error(‘invalid length’)

end;

P_Im = reshape(Im_linear,[],t);

function [Im_data, Im]=ReadImage(Im_Name,Height,Width)

Im_fp = fopen(Im_Name,‘rb’);

Im_data = fread(Im_fp,‘uint8’);

h = figure;

Im = uint8(reshape(Im_data,Height,Width));

imshow(Im);

title(‘Original Image’);

function y = powermod(a,z,n)

[ax,ay]=size(a);

a=mod(a,n);

if (z<0),

z=-z;

for j=1:ax,

for k=1:ay,

a(j,k)=invmodn(a(j,k),n);

end;

end;

end;

for j=1:ax,

for k=1:ay,

x=1;

a1=a(j,k);

z1=z;

while (z1 ~= 0),

while (mod(z1,2) ==0),

z1=(z1/2);

a1=mod((a1*a1), n);

end;

z1=z1-1;

x=x*a1;

x=mod(x,n);

end;

y(j,k)=x;

end;

end;

function y = invmodn( b,n);

n0=n;

b0=b;

t0=0;

t=1;

q=floor(n0/b0);

r=n0-q*b0;

while r>0,

temp=t0-q*t;

if (temp >=0),

temp=mod(temp,n);

end;

if (temp < 0),

temp= n — ( mod(-temp,n));

end;

t0=t;

t=temp;

n0=b0;

b0=r;

q=floor(n0/b0);

r=n0-q*b0;

end;

if b0 ~=1,

y=[];

disp(‘No inverse’);

else

y=mod(t,n);

end;

Here is the error:

??? Error using ==> reshape

To RESHAPE the number of elements must not change.

Error in ==> ImageSharing>ReadImage at 127 Im = uint8(reshape(Im_data,Height,Width));

Error in ==> ImageSharing at 24 [Im_linear, Im_Square] = ReadImage(Im_Name,Height,Width); Whenever i run this with an image i get the error in the reshape statement. Please help.

Skip to main content

Forum for Electronics

Forum for Electronics

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals… and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

  • Digital Design and Embedded Programming

  • Digital Signal Processing

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

[SOLVED] Matlab reshape error


  • Thread starter

    eldamar


  • Start date

    May 16, 2014

Status
Not open for further replies.

  • #1

Newbie level 5

Joined
Apr 24, 2014
Messages
10
Helped
2
Reputation

4

Reaction score
2
Trophy points
3
Activity points

68


Hi everyone. I am trying to write matlab programme for audio compression using DCT method. Here, I posted some of the my code.

%function result = compress_dct(X, window, num_components, coeff_bits)
[X1 , f ]=wavread('samplepiano.wav');
window=35208;
num_components=100;
coeff_bits=10;
num_win = 48;
%new=numel(X1);
%disp(new);
X = reshape(X1, window, num_win); % reshape so each window is a row
Y = dct(X); % applies dct to each row
[a, I] = sort(abs(Y), 'descend');
I = I(1:num_components, :);

….
Matlab gives such an error when I try to execute this programme and I didn’t understand why.Can you help me to fix this error,please? ( By the way, my wav file has 3379968 elements I found it with using numel(). )

Error using reshape
To RESHAPE the number of elements must not change.

Last edited by a moderator: May 17, 2014

  • #2

Full Member level 4

Joined
Oct 9, 2013
Messages
232
Helped
69
Reputation

142

Reaction score
73
Trophy points
1,318
Location

Sweden

Activity points

3,460


In your reshape operation, you are trying to create a (35208 x 48) matrix from a vector of length 3379968. Since 35208*48 = 1689984, this cannot be done.

If you only want to use the first 1689984 samples, you should write:

X = reshape(X1(1:window*num_win), window, num_win);

If you want to use all the data, you need to double num_win. In fact, since your data length divides exactly by 35208, a useful shortcut is to use «[]» for the other dimension:

X = reshape(X1, window, []);

Also, please note that your code comment is incorrect. In order to make each window a row, you must transpose the matrix (since reshape() operates column-wise):

X = reshape(X1, window, []).';

Last edited: May 17, 2014

  • #3

Newbie level 5

Joined
Apr 24, 2014
Messages
10
Helped
2
Reputation

4

Reaction score
2
Trophy points
3
Activity points

68


Thank you for your help, it is really helped me :))

Regards eldamar…

Status
Not open for further replies.

Similar threads

  • Digital Design and Embedded Programming

  • Digital Signal Processing

  • This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Error writing input to command
  • Error using mtimes inner matrix dimensions must agree
  • Error ошибка текущая транзакция прервана команды до конца блока транзакции игнорируются postgresql
  • Error writing fat
  • Error using mldivide matrix dimensions must agree

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии