如何在我的Matlab脚本中使向量长度相同?

时间:2022-03-16 16:12:52

I am trying to plot some data. The script I wrote below has worked fine before, but now I have no idea why it's not working.

我试图绘制一些数据。我在下面写的脚本以前工作得很好,但现在我不知道它为什么不起作用。

Here is the code:

这是代码:

x = [335,41,14,18,15,9,7,9,20607,5,5,143,3,5,72,134,2,28,172,3,72,173,280,186,20924,1,1,22,3,3,1,2,13,1,3,2,11,66,12983,176,123,192,64,258,182,123,299,58,198,7,113,342,72,8376,122,20,19,2,3,28,8,36,8,56,43,2,48,127,395,4664,186,46,236,219,258,69,203,189,169,72,100,78,109,46,112,3929,272,40,4,31,2,97,36,5,35,56,2,237,1672,256,224,28,163,341,151,263,157,397,94,380,173,75,87,272,1194,133,6,112,1,6,2,26,25,64,8,40,57,106,525,150,248,125,269,264,256,357,153,64,152,283,1,2,2,454,154,39,1,1,64,151,242,1,18,99,1,36,607,55,54,110,225,108,37,1,144,162,137,107,21,360,362,18,51,25,43,1,3,6,1,27,7,45,326,32,103,50,124,155,39,180,143,33,116,46,7,151,120,19,4,2,4,110,2,7,4,9,4,27,216,323,148,1,1,2,1,47,113,150,1,2,144,16,4827,1,1,1,14];
size = length(x);
disp(size);
z = 0;
for i = 1:size
    z = z + 1;
    y(i) = z;
end
scatter(x,y);

This code should ensure that y is of same length as x as we are only filling in y as long as x is (since we are using a for loop from 1 through to size, where size is basically the number of indices in x), but I keep getting this error. I checked with disp and it turns out that my x and y vectors have different lengths, x is 227 and y is 256. Can anyone help me out with this trivial issue?

这段代码应该确保y与x的长度相同,因为只要x是填充y的(因为我们使用从1到大小的for循环,其中size基本上是x中索引的数量),但我一直收到这个错误。我检查了disp,结果发现我的x和y向量有不同的长度,x是227,y是256.任何人都可以帮我解决这个微不足道的问题吗?

1 个解决方案

#1


2  

This is most likely because y was created to be a different size before you ran that piece of code you showed us. Somewhere in your script before you call this piece of code, y was created to be a 256 element vector and now you are reusing this variable in this part of the code by populating elements in the y vector. The variable x has 227 elements and the loop you wrote will change y's first 227 elements as you have looped for as many times as there are elements in x. However, the remaining 29 elements are still there from before. The reusing of the variable y is probably why your script is failing as now the sizes between both variables are not the same. As such, explicitly recreate y before calling scatter.

这很可能是因为在运行您向我们展示的那段代码之前,y被创建为不同的大小。在调用这段代码之前,在脚本的某处,y被创建为256元素向量,现在您通过在y向量中填充元素来重用此部分代码中的此变量。变量x有227个元素,你编写的循环将改变y的前227个元素,因为你循环的次数与x中的元素一样多。但是,剩下的29个元素仍然存在。重复使用变量y可能是你的脚本失败的原因,因为现在两个变量之间的大小不同。因此,在调用scatter之前显式重新创建y。

Actually, that for loop is not needed at all. The purpose of the loop is to create an increasing array from 1 up to as many elements as you have in x.

实际上,根本不需要for循环。循环的目的是创建一个增加的数组,从1增加到x中的元素数量。

Just do this instead:

只需这样做:

y = 1:size;

I also do not like that you are creating a variable called size. It is overshadowing the function size, which finds the number of elements in each dimension that the input array contains.

我也不喜欢你正在创建一个名为size的变量。它掩盖了函数大小,它查找输入数组包含的每个维度中的元素数量。

With the recommendations I've stated above, replace your entire code with this:

根据我上面提到的建议,用以下代码替换整个代码:

x = [335,41,14,18,15,9,7,9,20607,5,5,143,3,5,72,134,2,28,172,3,72,173,280,186,20924,1,1,22,3,3,1,2,13,1,3,2,11,66,12983,176,123,192,64,258,182,123,299,58,198,7,113,342,72,8376,122,20,19,2,3,28,8,36,8,56,43,2,48,127,395,4664,186,46,236,219,258,69,203,189,169,72,100,78,109,46,112,3929,272,40,4,31,2,97,36,5,35,56,2,237,1672,256,224,28,163,341,151,263,157,397,94,380,173,75,87,272,1194,133,6,112,1,6,2,26,25,64,8,40,57,106,525,150,248,125,269,264,256,357,153,64,152,283,1,2,2,454,154,39,1,1,64,151,242,1,18,99,1,36,607,55,54,110,225,108,37,1,144,162,137,107,21,360,362,18,51,25,43,1,3,6,1,27,7,45,326,32,103,50,124,155,39,180,143,33,116,46,7,151,120,19,4,2,4,110,2,7,4,9,4,27,216,323,148,1,1,2,1,47,113,150,1,2,144,16,4827,1,1,1,14];
numX = numel(x);
y = 1 : numX;
scatter(x,y);

The vector y is now explicitly created instead of reusing the variable that was created with a previous size in the past. It also uses the colon operator to explicitly create this sequence instead of using a for loop. That for loop is just not needed. numel determines the total number of elements for an input matrix. I don't like using length as a personal preference because it finds the number of elements in the largest dimension. This may work fine for vectors, but it has really made some hard to spot bugs in code that I've written in the past.

现在显式创建了向量y,而不是重用过去使用先前大小创建的变量。它还使用冒号运算符显式创建此序列,而不是使用for循环。那个for循环就不需要了。 numel确定输入矩阵的元素总数。我不喜欢使用长度作为个人偏好,因为它找到最大维度中的元素数量。这可能适用于矢量,但它确实很难发现我过去编写的代码中的错误。

#1


2  

This is most likely because y was created to be a different size before you ran that piece of code you showed us. Somewhere in your script before you call this piece of code, y was created to be a 256 element vector and now you are reusing this variable in this part of the code by populating elements in the y vector. The variable x has 227 elements and the loop you wrote will change y's first 227 elements as you have looped for as many times as there are elements in x. However, the remaining 29 elements are still there from before. The reusing of the variable y is probably why your script is failing as now the sizes between both variables are not the same. As such, explicitly recreate y before calling scatter.

这很可能是因为在运行您向我们展示的那段代码之前,y被创建为不同的大小。在调用这段代码之前,在脚本的某处,y被创建为256元素向量,现在您通过在y向量中填充元素来重用此部分代码中的此变量。变量x有227个元素,你编写的循环将改变y的前227个元素,因为你循环的次数与x中的元素一样多。但是,剩下的29个元素仍然存在。重复使用变量y可能是你的脚本失败的原因,因为现在两个变量之间的大小不同。因此,在调用scatter之前显式重新创建y。

Actually, that for loop is not needed at all. The purpose of the loop is to create an increasing array from 1 up to as many elements as you have in x.

实际上,根本不需要for循环。循环的目的是创建一个增加的数组,从1增加到x中的元素数量。

Just do this instead:

只需这样做:

y = 1:size;

I also do not like that you are creating a variable called size. It is overshadowing the function size, which finds the number of elements in each dimension that the input array contains.

我也不喜欢你正在创建一个名为size的变量。它掩盖了函数大小,它查找输入数组包含的每个维度中的元素数量。

With the recommendations I've stated above, replace your entire code with this:

根据我上面提到的建议,用以下代码替换整个代码:

x = [335,41,14,18,15,9,7,9,20607,5,5,143,3,5,72,134,2,28,172,3,72,173,280,186,20924,1,1,22,3,3,1,2,13,1,3,2,11,66,12983,176,123,192,64,258,182,123,299,58,198,7,113,342,72,8376,122,20,19,2,3,28,8,36,8,56,43,2,48,127,395,4664,186,46,236,219,258,69,203,189,169,72,100,78,109,46,112,3929,272,40,4,31,2,97,36,5,35,56,2,237,1672,256,224,28,163,341,151,263,157,397,94,380,173,75,87,272,1194,133,6,112,1,6,2,26,25,64,8,40,57,106,525,150,248,125,269,264,256,357,153,64,152,283,1,2,2,454,154,39,1,1,64,151,242,1,18,99,1,36,607,55,54,110,225,108,37,1,144,162,137,107,21,360,362,18,51,25,43,1,3,6,1,27,7,45,326,32,103,50,124,155,39,180,143,33,116,46,7,151,120,19,4,2,4,110,2,7,4,9,4,27,216,323,148,1,1,2,1,47,113,150,1,2,144,16,4827,1,1,1,14];
numX = numel(x);
y = 1 : numX;
scatter(x,y);

The vector y is now explicitly created instead of reusing the variable that was created with a previous size in the past. It also uses the colon operator to explicitly create this sequence instead of using a for loop. That for loop is just not needed. numel determines the total number of elements for an input matrix. I don't like using length as a personal preference because it finds the number of elements in the largest dimension. This may work fine for vectors, but it has really made some hard to spot bugs in code that I've written in the past.

现在显式创建了向量y,而不是重用过去使用先前大小创建的变量。它还使用冒号运算符显式创建此序列,而不是使用for循环。那个for循环就不需要了。 numel确定输入矩阵的元素总数。我不喜欢使用长度作为个人偏好,因为它找到最大维度中的元素数量。这可能适用于矢量,但它确实很难发现我过去编写的代码中的错误。