我有一个矢量,例如
vector = [1 2 3]
Run Code Online (Sandbox Code Playgroud)
我想在自己内部复制n次,即如果n = 3,它最终会:
vector = [1 2 3 1 2 3 1 2 3]
Run Code Online (Sandbox Code Playgroud)
如何为n的任何值实现此目的?我知道我可以做以下事情:
newvector = vector;
for i = 1 : n-1
newvector = [newvector vector];
end
Run Code Online (Sandbox Code Playgroud)
这看起来有点麻烦.更有效的方法?
在编写以下Matlab代码时:
for ii=1:n
x(ii) = foo( ii ); % foo is some function of ii that cannot be vectorized.
end
Run Code Online (Sandbox Code Playgroud)
我得到以下m-lint警告:
变量
x似乎在每次循环迭代时改变大小
我的问题:
这个问题是不能重复的这一个,因为它与预分配的更一般的问题,而是它的一个特定实例涉及.
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
## warning: product: automatic broadcasting operation applied
theta = theta - sum(X .* (X * theta - y))' .* (alpha / (m .* 2));
J_history(iter) = computeCost(X, y, theta);
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的作业,但我不要求你为我做这件事(我实际上认为我已经做过或接近过).我在提到播放的地方已经红了,但是我还是不明白,为什么我在这里收到警告?