Loop through a matrix with multiple elements with the same values

1 view (last 30 days)
Based on this answer given by Cedriv Wannaz, I would like to ask how to generalize the answer. I mean that I know than in the column exist multiple values but I do not know how many they are, but I want to extract the entries as shown in the answer. How do I iterate through the values in order to succeed that?

Answers (1)

Stephen23
Stephen23 on 30 Sep 2015
Edited: Stephen23 on 30 Sep 2015
The basic steps will be to:
  1. find how many values there are.
  2. repeat that code for each variable.
It seems that you are wanting each unique values in the first column of that matrix, in which case you can use unique to get a vector of the unique values:
V = unique(data(:,1));
Then loop over them using a for loop:
for k = numel(V):-1:1
V(k) % value of that loop iteration
... do whatever code with that value
out{k} = ... % save the results
end
Exactly how you save the results (or if they are saved at all) depends on you and what post-processing you want to perform. As you have not told us anything about the post-processing of this data after this step it is difficult to advise on the best way to store the output data.

Categories

Find more on Loops and Conditional Statements 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!