executing a loop most probably while

1 view (last 30 days)
Busy Bee
Busy Bee on 16 Jan 2018
Edited: Guillaume on 16 Jan 2018
I have created a function which groups a vast data. Here, for simplicity, I have used 20 data points. group(x,y) creates the required group (g1) and the next 'for' loop removes the elements of this group to create a new dataset. Again the group(x,y) creates the next group(g2) and the initial dataset is modified. I want to create a loop which will execute this till the dataset has zero elements,i.e. all the elements are grouped with group numbers,g1,g2 etc. I have tried to use while loop but I cannot change the group names, hence the error. Any help will be appreciated.
x = gallery('uniformdata',20,1,1);
y = gallery('uniformdata',20,1,1);
group(x,y);
g1=ans
for i=1:length(g1)
x(any(x==g1(i,1),2),:)=[];
y(any(y==g1(i,2),2),:)=[];
end
group(x,y);
g2=ans
for i=1:length(g2)
x(any(x==g2(i,1),2),:)=[];
y(any(y==g2(i,2),2),:)=[];
end
  3 Comments
Stephen23
Stephen23 on 16 Jan 2018
@Busy Bee:
  • Why do you not return any output from group ?
  • Do NOT use ans as a variable name.
  • Do not try to magically generate variable names g1, g2, etc:
Just use simpler and much more efficient indexing.
Busy Bee
Busy Bee on 16 Jan 2018
this is the answer i want
g1 =
0.1996 0.0736
0.4428 0.0015
0.8407 0.0159
0.9102 0.3281
0.9528 0.4493
0.9539 0.9641
g2 =
0.3759 0.1164
0.5982 0.0765
0.7041 0.2088
0.8368 0.2609
0.8986 0.5779
g3 =
0.3068 0.4931
0.5187 0.3469
0.5383 0.4436
0.7153 0.6566
g4 =
0.4290 0.4946
0.5253 0.8889
g5 =
0.0222 0.9105
0.3031 0.9285
g6 =
0.0345 0.9509

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 16 Jan 2018
Edited: Guillaume on 16 Jan 2018
First, some important notes
group(x, y);
g = ans;
Do not do this! This is just a more complicated and confusing way of simply doing
g = group(x, y);
Secondly, you create x and y as column vectors, but then have
x(any(x == g(i,1), 2), :) = [];
y(any(x == g(i,1), 2), :) = [];
which is designed for x and y matrices. If x and y are indeed vector then the above can be reduced simply to
x(x == g(i, 1)) = [];
y(y == g(i, 2)) = [];
In addition the i loop can be entirely removed, for x, y matrices:
x(any(ismember(x, g(:, 1)), 2)) == [];
y(any(ismember(y, g(:, 2)), 2)) == [];
and for x, y vectors simply:
x(ismember(x, g(:, 1))) = [];
y(ismember(y, g(:, 2))) = [];
Now to answer your question, first do not number variables, see Stephen's comment. Instead create a cell array that can easily be indexed:
x = gallery('uniformdata',20,1,1);
y = gallery('uniformdata',20,1,1);
g = {}; %better variable name required
count = 0;
while ~isempty(x)
count = count + 1;
g{count} = group(x, y);
x(ismember(x, g{count}(:, 1))) = [];
y(ismember(y, g{count}(:, 2))) = [];
end
g{i} is the ith group.

More Answers (0)

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!