How to Solve RESHAPE Function Error

I am trying to use a reshape function
If i use this syntax
plmprofile1=reshape(plmprofile1,90,180);
the function works well but i am trying to make this function more dynamic so when i created two integer variables nxint = 180 and nyint = 90
and using this syntax
plmprofile1=reshape(plmprofile1,nyint,nxint);
the code didn't work well and this error appeared
To RESHAPE the number of elements must not change.

1 Kommentar

That looks like it should work, but at the time of failure please show us the output of
size(plmprofile1)
nyint
nxint

Melden Sie sich an, um zu kommentieren.

Antworten (2)

James Tursa
James Tursa am 30 Jun. 2017

0 Stimmen

Either:
1) The total number of elements of plmprofile1 is different between the reshape(plmprofile1,90,180) call and the reshape(plmprofile1,nyint,nxint) call
or
2) The variables nyint and nxint really don't contain the values you think they do.
To debug this problem, do the following. At the command line prompt issue the following:
dbstop if error
Then run your code. When the error is encountered, the code will pause with all variables intact. Examine size(plmprofile1) and the values of nyint and nxint to see what is really going on.

3 Kommentare

Khaled AbdEldayem
Khaled AbdEldayem am 30 Jun. 2017
There is no different
but it works when i use nx and ny (they are float not integer values)
Khaled AbdEldayem
Khaled AbdEldayem am 30 Jun. 2017
the variables contain the values as 8 bit integers
It does not seem to make any difference in my tests whether the size variables are uint8 or double.
Please go through the debugging steps I indicated, showing us
size(plmprofile1)
nyint
nxint

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 1 Jul. 2017

0 Stimmen

It works fine for me, either with numbers or variables:
% Create an array with 16,000 elements.
plmprofile1 = rand(45, 360);
% Reshape to 90 by 180.
plmprofile1a = reshape(plmprofile1, 90, 180);
% Reshape to 90 by 180.
nxint = 180
nyint = 90
plmprofile1b = reshape(plmprofile1, nyint, nxint);
What did you do differently?

5 Kommentare

Khaled AbdEldayem
Khaled AbdEldayem am 1 Jul. 2017
nxint and nyint are float numbers not integer
while nxint and nyint in my code is a 8 bit integer variables
Image Analyst
Image Analyst am 1 Jul. 2017
So it's solved now, right? You understand what's going on, right? You can't take a set of, say, 10 elements and rearrange them into a matrix 4.123 columns wide by 18.334567 rows high. If you want to get a new matrix at some fractional columns and rows, then you'll have to use interp2(), griddedInterpolant(), or imresize(). You'll have to figure out what the new number of rows and columns will be, and they must be integers, for your new, interpolated matrix.
I think Khaled is talking about the difference between
nxint = 180
and
nxint = uint8(180)
which is something that makes no difference in my testing.
You could be right. When I said
nxint = 180
nyint = 90
and asked the difference, and he said "nxint and nyint are float numbers not integer" I assume he meant that he had fractional parts off the integer, because everyone knows (or should know) that saying the above means that the variables are floating point doubles, even if they store integer values. Though maybe Khaled didn't know that.
But like Walter, in my tests, with the original uploaded resolution I didn't encounter any black frames. If Khaled wants to dive into it more then he might have to open a case with tech support since neither of us can reproduce his problem.
Walter Roberson
Walter Roberson am 1 Jul. 2017
I could not read the file at all in MATLAB on my Mac.

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 30 Jun. 2017

Kommentiert:

am 1 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by