loop for matlab array
1 次查看(过去 30 天)
显示 更早的评论
i have a problem with for loop.we have a vector a=[0 1 1 1 0] and a vector b=[1 1 1 1 1]
for i = 1:length(a)
if a(i)==0
c = [-1.*b] ;
else if a(i)==1
c = [1.* b];
end
end
the loop works only for a(1)=0 and stops.
0 个评论
回答(3 个)
Geoff Hayes
2016-11-23
Try removing the space between the else if. Re-write this as
if a(i)==0
c = [-1.*b] ;
elseif a(i)==1
c = [1.* b];
end
4 个评论
Image Analyst
2016-11-23
Then just do
handles.edit1.String = sprintf('%d ', c);
This will convert the vector to a string and put it into the edit text box on your GUI.
Image Analyst
2016-11-23
Maybe you wanted elseif instead of "else if". This works and does several iterations:
a=[0 1 1 1 0]
b=[1 1 1 1 1]
for i = 1:length(a)
if a(i)==0
c = [-1.*b]
elseif a(i)==1
c = [1.* b]
end
end
Of course there are better ways to do that and no experienced MATLAB programmer would do that operation like that.
3 个评论
Image Analyst
2016-11-23
They will all be displayed because there is no semicolon at the end of the line. Did you actually try it and look in the command window?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!