Set length to content of cell, not the array dimensions

2 views (last 30 days)
I was looking to make a vector of zeroes of length of a variable but when I code it as follows:
Variable1 = zeros(length(Numberofzeroesrequired)
It returns a vector of zeroes the size of the array, is there anyway to code it so it returns zeroes of whatever value Numberofzeroesrequired is. E.g. if Numberofzeroesrequired is a 1x1 array with a single number 6 in it, it would return zeroes of length 6.

Answers (2)

Star Strider
Star Strider on 16 Feb 2015
Try this:
Numberofzeroesrequired = 6;
Variable1 = zeros(Numberofzeroesrequired,1) % Produces A 6x1 Column Vector
The zeros, ones, and nan functions (and perhaps others), return a (NxN) matrix if they are given only one argument, N. To create a vector, specify that the second argument is 1. If the first argument is 1, it will return a (1xN) row vector, if the second argument is 1, it will return a (Nx1) column vector. So put the 1 to produce the size vector you want.
  2 Comments
LukeC
LukeC on 16 Feb 2015
Thanks for your answer but when I use that code I receive the error:
"Error using zeros Size inputs must be numeric."
Star Strider
Star Strider on 16 Feb 2015
They are in my example. It produces a (6x1) double vector of zeros. Copy my code and paste it to the MATLAB editor and run it.
What are you passing to the zeros function in your code? Please past all of the relevant code in your next Comment.

Sign in to comment.


the cyclist
the cyclist on 16 Feb 2015
Do you mean
Variable1 = zeros(Numberofzeroesrequired,1)
?

Community Treasure Hunt

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

Start Hunting!