adding elements in a loop into an array

6 次查看(过去 30 天)
I need to ask the user for the number of windows in a house give the sizes of all the windows. To do this, I am trying to use loops to determine how many times to ask for a window's size and add the size into an array. My problem is that when the code is ran, x = [], and doesn't contain the elements I inputed.
Here's me code:
num_windows = input('How many windows?: ');
x = [];
for k = 1:num_windows
window_size = input('Window size?: ');
x = x + window_size;
end

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-11-28
Use x=[x;window_size];
or a better way,
if num_windwows>0
x=zeros(num_windwows,1);
else
error('Invalid number of windows');
end
Then inside the for-loop
x(k)=window_size;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by