Set length to content of cell, not the array dimensions

1 次查看(过去 30 天)
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.

回答(2 个)

Star Strider
Star Strider 2015-2-16
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 个评论
LukeC
LukeC 2015-2-16
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 2015-2-16
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.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by