对于下面的代码,我想要输入值b
和k
,n
次:
syms Q i w h2 h1 k
% n is number of layers for computation
n = input (' ')
for j = 1:n
k1 = input (' '); b1 = input (' ');
% up to...
k(n) = input (' '); b(n) = input (' ');
% so that i want to use the various inputs of b and k to calculate Q.
Qi=( -b(i) * k(i) )*((h2-h1)/w)
Q=symsum(Qi, i, 1, 3)
答案 0 :(得分:2)
在循环中,您可以提示输入并将其存储为k
和b
值数组
% Pre-allocate k and b arrays
k = zeros(1, n);
b = zeros(1, n);
for ind = 1:n
% Prompt for the next k value
k(ind) = input('');
% Prompt for the next b value
b(ind) = input('');
end
或者,您可以直接提示用户输入数组
k = input('Please enter an array for k in the form [k1, k2, k3]');
b = input('Please enter an array for b in the form [b1, b2, b3]');
使用这些数组,您可以为Q
和k
的每个值计算b
Q = (-b .* k)*((h2-h1)/w);