Obtaining the index of elements that belong to group

I have 4 groups
A =[1;2;3];
B = [4;5];
C = [6;7];
D = [8,9];
I put all of these value together
all_val = [1;2;3;4;5;6;7;8;9];
From all_val I choose 2 elements.
idx = datasample(all_val, 2,'Replace',false);
I want to know the chosen idx is belong to which groups.
I do not know how to write this code.
Result should be something like:
i=1 i=2
third index of A first index of B
second index of c second index of D

 Accepted Answer

See the second output of datasample to get the selected index.
The problem you're having is that you don't know which rows of all_val corresponds to which groups. When you create all_val you need to keep track of the group IDs.
Demo:
A =[1;2;3];
B = [4;5];
C = [6;7];
D = [8;9];
abcd = {A;B;C;D};
all_vals = cell2mat(abcd); % <-- Create all_vals this way
n = cellfun(@numel,abcd);
g = repelem(1:numel(abcd),1,n)';
g(:,2) = cell2mat(arrayfun(@(n){(1:n)'},n));
g is a nx2 grouping variable where the first column indicates which variable (A,B,C,D) each value in all_vals comes from. The second column indicates the index within that variable.
g =
1 1
1 2
1 3
2 1
2 2
3 1
3 2
4 1
4 2
This is where the 2nd output to datasample comes into play.
[y, idx] = datasample(all_val, 2,'Replace',false);
If idx is [2,8] then the selected variable comes from
g(idx,:)
% ans =
% 1 2 % 1st variable (A), 2nd row
% 4 1 % 4th variable (D), 1st row

5 Comments

I was hoping you'd take it the rest of the way but I've updated my answer so it's more complete.
Thank you for taking the time to answer my question.
Instead of this answer
% ans =
% 1 2 % 1st variable (A), 2nd row
% 4 1 % 4th variable (D), 1st row
How I can get this
% ans =
% A 2 % 1st variable (A), 2nd row
% D 1 % 4th variable (D), 1st row
g = repelem(["A","B","C","D"],1,n)';
But then when you add the 2nd column, those numbers will be strings too. So you probably want a cell array.
g= repelem({'A','B','C','D'},1,n)';
g(:,2) = num2cell(cell2mat(arrayfun(@(n){(1:n)'},n)));
But now I'm worried about what you're going to do with those letters. If you're going to use them as labels, that's perfectly fine. If you're planning on accessing the original variables with the same name as the letters, that's a really bad idea. It's call "dynamic variable naming" and if you search for that term in this forum you'll find hundreds of warnings aginst it.
Thank you for your suggestion. I want to use them as labels.
Suppose I have the additional data.
e = [1,2; 2,3; 3,1];
v = (1:max(max(3)))';
%% first grouping
A =[1;2;3];
B = [4;5;6];
C = [7;8;9];
D = [10;11;12];
%% second grouping
los = {A;C};
def = {B;D};
Rest of the code
abcd = {A;B;C;D};
all_vals = cell2mat(abcd); % <-- Create all_vals this way
n = cellfun(@numel,abcd);
g = repelem({'A','B','C','D'},1,n)';
g(:,2) = repelem({'los','def','los','def'},1,n)';
I want to add another column to g like this
g =
12×3 cell array
{'A'} {'los'} {[1,2]}
{'A'} {'los'} {[2,3]}
{'A'} {'los'} {[3,1]}
{'B'} {'def'} {[1]}
{'B'} {'def'} {[2]}
{'B'} {'def'} {[3]}
{'C'} {'los'} {[1,2]}
{'C'} {'los'} {[2,3]}
{'C'} {'los'} {[2,3]}
{'D'} {'def'} {[1]}
{'D'} {'def'} {[2]}
{'D'} {'def'} {[3]}
I mean
first element of los should be ---> e(1)=(1,2)
second element of los should be ---> e(2)=(2,3)
third element of los should be ---> e(3)=(3,1)
first element of def should be ---> v(1)=1
second element of def should be ---> v(2)=2
third element of def should be ---> v(3)=3
I do not know how to relate e to los and v to def. How to change bellow code?
g(:,3) = num2cell(cell2mat(arrayfun(@(n){(1:n)'},n)));

Sign in to comment.

More Answers (0)

Tags

Asked:

NA
on 8 Sep 2020

Commented:

on 9 Sep 2020

Community Treasure Hunt

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

Start Hunting!