Matlab:如何实现动态向量

发布于 2024-10-09 10:29:21 字数 422 浏览 0 评论 0原文

我指的是这样的例子 我有一个函数来分析向量“输入”的元素。如果这些元素具有特殊属性,我会将它们的值存储在向量“输出”中。 问题是,一开始我不知道它需要存储在“输出”中的元素数量,所以我不知道它的大小。 我有一个循环,在里面我绕过向量,通过索引“输入”。当我考虑特殊时,该向量的某些元素捕获“输入”的值,并通过如下句子将其存储在向量“输出”中:

For i=1:N %Where N denotes the number of elements of 'input'
...
output(j) = input(i);
...
end

问题是,如果我之前不“声明”,我会收到错误输出'。我不喜欢在到达循环之前“声明”“输出”,因为输出=输入,因为它存储我不感兴趣的输入值,我应该想一些方法来删除我存储的所有不感兴趣的值与我相关。 有人向我解释这个问题吗? 谢谢。

I am refering to an example like this
I have a function to analize the elements of a vector, 'input'. If these elements have a special property I store their values in a vector, 'output'.
The problem is that at the begging I don´t know the number of elements it will need to store in 'output'so I don´t know its size.
I have a loop, inside I go around the vector, 'input' through an index. When I consider special some element of this vector capture the values of 'input' and It be stored in a vector 'ouput' through a sentence like this:

For i=1:N %Where N denotes the number of elements of 'input'
...
output(j) = input(i);
...
end

The problem is that I get an Error if I don´t previously "declare" 'output'. I don´t like to "declare" 'output' before reach the loop as output = input, because it store values from input in which I am not interested and I should think some way to remove all values I stored it that don´t are relevant to me.
Does anyone illuminate me about this issue?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2024-10-16 10:29:21

for循环中的逻辑有多复杂?

如果很简单,这样的事情会起作用:

output = input ( logic==true )

或者,如果逻辑很复杂并且您正在处理大向量,我会预先分配一个向量来存储是否保存元素。这是一些示例代码:

N = length(input); %Where N denotes the number of elements of 'input'
saveInput = zeros(1,N);  % create a vector of 0s
for i=1:N
    ...
    if (input meets criteria)
        saveInput(i) = 1;
    end
end
output = input( saveInput==1 ); %only save elements worth saving

How complicated is the logic in the for loop?

If it's simple, something like this would work:

output = input ( logic==true )

Alternatively, if the logic is complicated and you're dealing with big vectors, I would preallocate a vector that stores whether to save an element or not. Here is some example code:

N = length(input); %Where N denotes the number of elements of 'input'
saveInput = zeros(1,N);  % create a vector of 0s
for i=1:N
    ...
    if (input meets criteria)
        saveInput(i) = 1;
    end
end
output = input( saveInput==1 ); %only save elements worth saving
咋地 2024-10-16 10:29:21

简单的解决方案是:

% if input(i) meets your conditions
output = [output; input(i)]

虽然我不知道这是否具有良好的性能

The trivial solution is:

% if input(i) meets your conditions
output = [output; input(i)]

Though I don't know if this has good performance or not

Spring初心 2024-10-16 10:29:21

如果 N 不是太大而导致内存问题,您可以将 output 预先分配给与 input 大小相同的向量>,并在循环结束时删除所有无用的元素。

output = NaN(N,1);
for i=1:N
...
output(i) = input(i);
...
end
output(isnan(output)) = [];

有两种选择:

如果output被分配了N的大小,或者如果您不知道的大小上限,那么output会太大输出,您可以执行以下操作

lengthOutput = 100;
output = NaN(lengthOutput,1);
counter = 1;
for i=1:N
   ...
   output(counter) = input(i);
   counter = counter + 1;
   if counter > lengthOutput
       %# append output if necessary by doubling its size
       output = [output;NaN(lengthOutput,1)];
       lengthOutput = length(output);
   end
end
%# remove unused entries
output(counter:end) = [];

最后,如果 N 很小,则完全可以调用

output = [];
for i=1:N
   ...
   output = [output;input(i)];
   ...
end

注意,如果 N 变大(例如 >1000),性能会急剧下降。

If N is not too big so that it would cause you memory problems, you can pre-assign output to a vector of the same size as input, and remove all useless elements at the end of the loop.

output = NaN(N,1);
for i=1:N
...
output(i) = input(i);
...
end
output(isnan(output)) = [];

There are two alternatives

If output would be too big if it was assigned the size of N, or if you didn't know the upper limit of the size of output, you can do the following

lengthOutput = 100;
output = NaN(lengthOutput,1);
counter = 1;
for i=1:N
   ...
   output(counter) = input(i);
   counter = counter + 1;
   if counter > lengthOutput
       %# append output if necessary by doubling its size
       output = [output;NaN(lengthOutput,1)];
       lengthOutput = length(output);
   end
end
%# remove unused entries
output(counter:end) = [];

Finally, if N is small, it is perfectly fine to call

output = [];
for i=1:N
   ...
   output = [output;input(i)];
   ...
end

Note that performance degrades dramatically if N becomes large (say >1000).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文