Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D matrix has wrong dimensions

Subject: 3D matrix has wrong dimensions

From: Hermano Cappa

Date: 2 Dec, 2011 15:47:08

Message: 1 of 7

Hi,

I want to create the following 3D matrix as follows:

Nx = 50;
Nz = 100;
Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab

P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x 100)

I get the following warning: Size vector should be a row vector with integer elements.

size(P)

ans =

    55 50 100

Why I don't get 56 rows?

Subject: 3D matrix has wrong dimensions

From: ade77

Date: 2 Dec, 2011 16:24:08

Message: 2 of 7

"Hermano Cappa" wrote in message <jbartr$spj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I want to create the following 3D matrix as follows:
>
> Nx = 50;
> Nz = 100;
> Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab
>
> P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x 100)
>
> I get the following warning: Size vector should be a row vector with integer elements.
>
> size(P)
>
> ans =
>
> 55 50 100
>
> Why I don't get 56 rows?

The warning is valid, but harmless. Normally, your the function "zeros", expects your Nx, Ny and Nz to be Integer(no decimal ), however, your Ny has a decimal part (.00), of course it does not matter, but MATLAB just gives u the warning to let u know there is a decimal part to it(though 56 = 56.00)......
but u should be careful because of floating number issue in your machine. Instead simply use Ny = 56, when possible.

Subject: 3D matrix has wrong dimensions

From: Hermano Cappa

Date: 2 Dec, 2011 16:35:09

Message: 3 of 7

Ade77,

Yes, I thought also that this was the reason for the warning, but why do I get 55 rows in my P matrix instead of 56?



"ade77 " <ade100a@gmail.com> wrote in message <jbau38$7sf$1@newscl01ah.mathworks.com>...
> "Hermano Cappa" wrote in message <jbartr$spj$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I want to create the following 3D matrix as follows:
> >
> > Nx = 50;
> > Nz = 100;
> > Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab
> >
> > P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x 100)
> >
> > I get the following warning: Size vector should be a row vector with integer elements.
> >
> > size(P)
> >
> > ans =
> >
> > 55 50 100
> >
> > Why I don't get 56 rows?
>
> The warning is valid, but harmless. Normally, your the function "zeros", expects your Nx, Ny and Nz to be Integer(no decimal ), however, your Ny has a decimal part (.00), of course it does not matter, but MATLAB just gives u the warning to let u know there is a decimal part to it(though 56 = 56.00)......
> but u should be careful because of floating number issue in your machine. Instead simply use Ny = 56, when possible.

Subject: 3D matrix has wrong dimensions

From: ade77

Date: 2 Dec, 2011 16:47:08

Message: 4 of 7

"Hermano Cappa" wrote in message <jbartr$spj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I want to create the following 3D matrix as follows:
>
> Nx = 50;
> Nz = 100;
> Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab
>
> P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x 100)
>
> I get the following warning: Size vector should be a row vector with integer elements.
>
> size(P)
>
> ans =
>
> 55 50 100
>
> Why I don't get 56 rows?

haha....
if u do format long
>Ny u will get
 55.999999999999993

meaning MATLAB treats it as 55, because MATLAB expects integer as the argument.
that is why u will have to be careful. To be on the safe side, u might use:
round(Ny)

zeros(round(Ny), Nx, Nz)

Subject: 3D matrix has wrong dimensions

From: Doug Schwarz

Date: 2 Dec, 2011 16:50:41

Message: 5 of 7

On 12/2/2011 10:47 AM, Hermano Cappa wrote:
> Hi,
>
> I want to create the following 3D matrix as follows:
>
> Nx = 50;
> Nz = 100;
> Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab
>
> P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x 100)
>
> I get the following warning: Size vector should be a row vector with
> integer elements.
>
> size(P)
>
> ans =
>
> 55 50 100
>
> Why I don't get 56 rows?

5.5e-3 / 0.1e-3 is not exactly 56. Because of the limitations of
floating-point arithmetic, the quotient is slightly less than 56.
MATLAB uses the integer part of Ny which is why you get 55 rows and the
warning about non-integer elements.

Use round(5.5e-3/0.1e-3) + 1 instead.


--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: 3D matrix has wrong dimensions

From: Steven_Lord

Date: 2 Dec, 2011 16:52:05

Message: 6 of 7



"Hermano Cappa" <cappasteven@gmail.com> wrote in message
news:jbartr$spj$1@newscl01ah.mathworks.com...
> Hi,
>
> I want to create the following 3D matrix as follows:
>
> Nx = 50;
> Nz = 100;
> Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab

No. It's _displayed_ as 56, but it's not _equal to_ 56 due to roundoff
error.

ifNyIs56ThenThisIsZero = 56-Ny % spoiler alert: it's not exactly 0

This is the reason for both the warning you receive when you try to use Ny
as input to ZEROS and the result of that ZEROS call being a size different
than you expect.

There are several solutions you could use: use one of the rounding functions
(ROUND, CEIL, FLOOR, FIX) to force Ny to be an exact integer value; divide
the number of elements you desire by Nx*Nz (assuming that number of elements
is an exact integer multiple of Nx*Nz); create your array as a vector with
the correct number of elements and RESHAPE it, allowing RESHAPE to determine
the appropriate value for Ny; etc.

numElements = Nx*Nz*56;
A = zeros(1, numElements);
B = reshape(A, [], Nx, Nz);

I would problem employ one of the latter two approaches if I needed to do
this in my code.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: 3D matrix has wrong dimensions

From: Tim McBrayer

Date: 2 Dec, 2011 16:55:15

Message: 7 of 7

Your value for Ny does not equal 56:

 >> format longE
 >> Ny = (5.5e-3/0.1e-3)+1
Ny =
      5.599999999999999e+01
 >> Ny == 56
ans =
      0

The zeros function is apparently using floor rounding to change the double to an integer.
  One solution is to explicitly convert Ny to an integer.

 >> Ny = int32((5.5e-3/0.1e-3)+1)
Ny =
           56
 >> Ny == 56
ans =
      1

See:
 
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

for why your original value for Ny is not == 56.


On 12/02/11 11:35, Hermano Cappa wrote:
> Ade77,
>
> Yes, I thought also that this was the reason for the warning, but why do
> I get 55 rows in my P matrix instead of 56?
>
>
>
> "ade77 " <ade100a@gmail.com> wrote in message
> <jbau38$7sf$1@newscl01ah.mathworks.com>...
>> "Hermano Cappa" wrote in message
>> <jbartr$spj$1@newscl01ah.mathworks.com>...
>> > Hi,
>> > > I want to create the following 3D matrix as follows:
>> > > Nx = 50;
>> > Nz = 100;
>> > Ny = (5.5e-3/0.1e-3)+1; %which is equal to 56.0000 in Matlab
>> > > P = zeros(Ny,Nx,Nz); % this should give a 3D matrix of (56 x 50 x
>> 100)
>> > > I get the following warning: Size vector should be a row vector
>> with integer elements.
>> > > size(P)
>> > > ans =
>> > > 55 50 100
>> > > Why I don't get 56 rows?
>>
>> The warning is valid, but harmless. Normally, your the function
>> "zeros", expects your Nx, Ny and Nz to be Integer(no decimal ),
>> however, your Ny has a decimal part (.00), of course it does not
>> matter, but MATLAB just gives u the warning to let u know there is a
>> decimal part to it(though 56 = 56.00)......
>> but u should be careful because of floating number issue in your
>> machine. Instead simply use Ny = 56, when possible.


--
Tim McBrayer
MathWorks

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us