简体   繁体   English

应用于值数组时,索引超出矩阵维

[英]Index exceeds matrix dimension when applied to an array of values

I wrote the following problem in Matlab to get the first 3 digits of a number. 我在Matlab中编写了以下问题,以获取数字的前3位数字。

Let x be a real number. 令x为实数。

function [y]=mifl(x) % mifl=my float
  s=num2str(x);
  y=sscanf(s(1:4),'%f');
end 

So function mifl returns the first 3 digits of a number. 因此,函数mifl返回数字的前3位。 For example, 例如,

mifl(pi)=3.14

But when I tried to apply this function to all the values of the vector v I got "Index exceeds matrix dimensions". 但是,当我尝试将此函数应用于向量v的所有值时,得到了“索引超出矩阵维数”。 I can't figure out why. 我不知道为什么。

I used 我用了

v=linspace(0.1, 99.9, 1000);
w=[]
  for i=1:5
    w(i)=mifl(v(i))
  end

That's when I get the "Index exceeds matrix dimensions". 那就是我得到“索引超出矩阵维数”的时候。

At the end, what I want is, given a vector 最后,我想要的是给定向量

v=linspace(0.1, 99.9, 1000); 

to get a vector 得到一个向量

w=[mifl(0.1),...mifl(99.9)]

The reason is because you are specifying a number in your function that doesn't have three significant digits. 原因是因为您在函数中指定的数字没有三位有效数字。 Specifically, try 0.1 from your vector. 具体来说,请尝试从向量中获取0.1 This doesn't have 3 digits and so you get an out of bounds error because you're assuming it does. 它没有3位数字,因此您会出界错误,因为您假设它有。

As such, in your function, check the length of the string and ensure that there are 4 characters to extract. 因此,在函数中,请检查字符串的长度,并确保要提取4个字符。 If not, then get whatever is available: 如果没有,则获取可用的任何内容:

function [y]=mifl(x) % mifl=my float
  s=num2str(x);
  m = min(numel(s), 4); %// Change
  y=sscanf(s(1:m),'%f');
end 

If you try the above, your code should now work. 如果您尝试上述操作,您的代码现在应该可以运行了。


I'd like to also suggest that you pre-allocate your arrays before populating them for speed. 我还建议您在填充数组以提高速度之前预先分配它们。

Specifically: 特别:

v=linspace(0.1, 99.9, 1000);
w=zeros(numel(v),1);
for i=1:numel(v)
   w(i)=mifl(v(i));
end

w is initialized to be an array of 0s that is as long as v , then we'll go through each value in v and call mifl , then store this result in the corresponding location in w . w初始化为一个与v一样长的0数组,然后我们遍历v每个值并调用mifl ,然后将结果存储在w中的相应位置。

You can use Matlabs rand function. 您可以使用Matlabs rand函数。

y = round(x,3,'significant');

This will return the first 3 significant digits of each number in a vector x . 这将返回向量x中每个数字的前3个有效数字。 This code will also be easy for another person to comprehend since round is a built in Matlab function, which is used for rounding. 由于round是内置的Matlab函数(用于舍入),因此其他人也很容易理解该代码。 The argument 'significant' should be clear enough. 'significant'的论点应该足够清楚。 This will also work for numbers larger than 100, which makes it more general. 这也适用于大于100的数字,这使其更通用。 That is of course if it was 3 significant digits you were after and not the first three digits in the number. 当然,如果您要输入的是3个有效数字,而不是数字的前3个数字,那是当然的。 In that case this will not work. 在这种情况下,这将不起作用。 The number 1001 would then be 1000 and not 100, which your solution would give. 那么数字1001将是1000,而不是您的解决方案给出的100。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配给结构数组时在Matlab中“索引超过矩阵维数” - “Index exceeds matrix dimensions” in Matlab when assigning to a struct array 为什么即使索引值超过给定的数组大小,也会显示此数组索引元素而不是引发错误? - why is this array index element is being displayed instead of throwing an error, even when the index values exceeds the given array size? Matlab:索引超出矩阵尺寸 - Matlab: Index exceeds matrix dimensions 创建带状对角矩阵时,spdiags出现“索引超出矩阵尺寸”错误? - “Index exceeds matrix dimensions” error with spdiags when creating band diagonal matrix? 根据第一维第一个索引中的值替换数组值 - replace array values according to values in the first index in the first dimension .forEach索引超出数组的长度 - .forEach index exceeds length of array 避免“索引超出矩阵尺寸”的功能 - Function to avoid 'Index exceeds matrix dimensions' Julia `mapslices` 当 output 尺寸超过输入尺寸时 - Julia `mapslices` when output dimension exceeds input dimension 检查矩阵中的值是否与数组中的值匹配,否则返回矩阵索引 - Check if values in matrix matches with values from array and return the matrix index if not 当在同一索引下给定多个值时,作为数组工作但自动增长到第二维的数据结构是什么? - What's the data structure that works as an array but grows automatically into 2nd dimension when given multiple values under same index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM