Pick a random column according to specific class data
1 次查看(过去 30 天)
显示 更早的评论
I have matrix lets say
1 1 2 2 3 3 4 4
5 8 3 7 2 8 3 9
4 6 8 2 3 4 6 7
2 3 6 7 3 4 6 8
I want to randomly pick columns from each first row class [1 2 3 4]
0 个评论
回答(1 个)
Image Analyst
2023-3-25
Try randi
A = [...
1 1 2 2 3 3 4 4
5 8 3 7 2 8 3 9
4 6 8 2 3 4 6 7
2 3 6 7 3 4 6 8];
[rows, columns] = size(A);
% Get random column indexes
randomColumns = randi(columns, rows, 1)
% Get values of A there
randomValues = zeros(rows, 1);
for row = 1 : rows
randomValues(row) = A(row, randomColumns(row));
end
randomValues
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!