For loop indexing issue

41 views (last 30 days)
Heather Smith
Heather Smith on 1 Jun 2015
Commented: Star Strider on 1 Jun 2015
The relevant code is:
for ii = 1:v
if v(ii) ~= a
y = [v(ii)]
elseif v(ii) == a
y = [y b c]
end
w = y
end
v being a vector of unknown length, w being the output of the function. Anyway, the problem is it's only checking for ii = 1; all other values of ii are being ignored. The code runs without errors- it's just running incorrectly. (If you can't tell I'm new to matlab, probably very obvious answer...)
  2 Comments
Lucas Carvalho
Lucas Carvalho on 1 Jun 2015
Hello Heather!
Since you are writing just one number for indexing (ii = 1), it will only evaluate the code for this situation. Try writing this:
for ii = 1:length(v)
Lucas Carvalho
Lucas Carvalho on 1 Jun 2015
Edited: Lucas Carvalho on 1 Jun 2015
By the way, you can see the definition of length() function and any other function on Matlab using the command help. For example:
help length

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 1 Jun 2015
You probably want to do:
for ii = 1:length(v)
however that’s not the only problem, since ‘w’ will return only the result of the last iteration of the loop. I don’t know what you’re doing, but since in each iteration ‘w’ can be either one or three elements long, you probably want to use a cell array to store the output:
w{ii} = y;
  3 Comments
Heather Smith
Heather Smith on 1 Jun 2015
Thanks, I didn't catch my error with w! There's another problem now though; I'm wanting it to store all previous iterations in y, so if there were three iterations it could be [y b c y b c y b c]; now, each time, it's writing over y and only giving a final output of y b c. This is before it gets to w = y, and using a cell array got a strange answer that I don't even understand: [1x2 double] [2] [3] the answer should be: [4,5,2,3]
Star Strider
Star Strider on 1 Jun 2015
My pleasure!
I thought you were saving everything in ‘w’, and ‘y’ was just a temporary variable in the loop.
The way I read your code (likely the way MATLAB reads it as well), your loop iterates through the elements of ‘v’, and if that element is not equal to ‘a’, it stores the current value of ‘v’ in ‘y’. If that element is equal to ‘a’, it stores the triplet [y b c] in ‘y’ (I assume ‘b’ and ‘c’ are defined elsewhere, and you also initialised ‘y’ before the first iteration). At the end of that iteration, it stores ‘y’ for that iteration as the ii-th element of ‘w’.
You didn’t describe what you want to do, so I have no idea if your code does it.
I created ‘w’ as a cell array, so it contains one assignment of ‘y’ for each iteration of the loop. You would access the fifth element of ‘w’ as: w{5} for instance (note the curly {} brackets), so all the data are there. Because every element of ‘w’ is potentially a different size, the cell array is necessary.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!