フィルターのクリア

selection of data from matrix

27 ビュー (過去 30 日間)
MUKESH KUMAR
MUKESH KUMAR 2017 年 11 月 17 日
編集済み: MUKESH KUMAR 2017 年 11 月 18 日
I have a dataset lets say a matrix of 100 rows and 5 columns, each row represents a dataset.Now I want to select 5 any rows(datasets) initially, then further I want to select 4 rows(dataset) except initially 5 rows should not be repeated, then next I want to select 10 rows except for initially selected rows (those 9 rows should not be repeated here) in before and so on..... one thing to remember that one rows should be selected once here.

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 17 日
data = randi([-41 78],100,5);
ii = randperm(size(data,1));
k = [5 4 10];
out = mat2cell(data(ii(1:sum(k)),:),k,size(data,2));
  1 件のコメント
MUKESH KUMAR
MUKESH KUMAR 2017 年 11 月 18 日
編集済み: MUKESH KUMAR 2017 年 11 月 18 日
short and sweet answer, thanks

サインインしてコメントする。

その他の回答 (3 件)

KL
KL 2017 年 11 月 17 日
編集済み: KL 2017 年 11 月 18 日
One easy alternative is to shuffle the row of your matrix first and then extract them from top to bottom,
data = [(1:100).' rand(100,5)]; %dummy data
indx = randperm(size(data,1));
data = data(indx,:);
numberofRowsWanted = 4;
data_ext = data(1:numberofRowsWanted,:);
Another alternative is to split and put them all in a cell array.
data = [(1:100).' rand(100,5)]; %dummy data
indx = randperm(size(data,1)); %generate random indices without repeating
splits = [4,5,10]; %how many rows you want to extraxt each time, extend it as you wish
%now, use arrayfun to extract those rows and store them in a cell array.
data_ext = arrayfun(@(x,y) data(indx(x:y),:),[1 splits+1],[splits(1) splits(1:end-1)+splits(2:end)],'uni',0);

KSSV
KSSV 2017 年 11 月 17 日
Check the below example code:
K = 1:100 ;
pick = [5 4 9 10] ;
for i = 1:length(pick)
take = randsample(K,pick(i))
% remove the selected numbers
K = setdiff(K,take) ;
end
  2 件のコメント
MUKESH KUMAR
MUKESH KUMAR 2017 年 11 月 17 日
if the size of K matrix like 100*5 then this code doesn't work.any further solution for it , Thanks
KL
KL 2017 年 11 月 18 日
It indeed works, in fact all of the solutions here do what you want, they are just examples. Instead of just copy-pasting, try and understand how it works.

サインインしてコメントする。


Guillaume
Guillaume 2017 年 11 月 17 日
There are many ways you could do this. You could keep track of the row you've selected and exclude them from the set of rows to select from, but probably the simplest way would be to remove the rows you've selected from your dataset.
dataset = rand(100, 5); %demo dataset
while ~isempty(dataset)
%select 5 rows at random:
numrowstoselect = 5; %adjust as required
selectedrows = randperm(size(dataset, 1), min(numrowstoselect, size(dataset, 1));
selecteddataset = dataset(selectedrows, :);
%remove selected rows from dataset so they can't be selected again
dataset(selectedrows, :) = [];
end

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by