How do I make an array with a for loop, when I doesn't use integers?

1 view (last 30 days)
I have a for loop where I get the error:
Attempted to access final(1.5,:); index must be a positive integer or logical.
Error in L4_Change (line 76)
final(I,:) = Y2_final(end,:); % Save Last Value Of Each ‘Y2’
My code is
for I = 1:0.5:4
(alot lot of code inbetween)
final(I,:) = Y2_final(end,:); % Save Last Value Of Each ‘Y2’
end
The code works if I use I = 1:4 instead of I = 1:0.5:4. I need the extra steps in between. Is there a way to solve this?

Answers (1)

Image Analyst
Image Analyst on 17 Dec 2015
I don't know what you really intend to do. This is the closest thing I can think of:
myValues = 1 : 0.5 : 4;
for k = 1 : length(myValues)
thisValue = myValues(k); % Extract the k'th index.
% Then use thisValue somehow, if you need to,
% but not as an index. Use k instead.
% ....more code......then...
% Store last row of Y2_final in the k'th row of final.
final(k,:) = Y2_final(end,:);
end

Community Treasure Hunt

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

Start Hunting!