How store vector output in matrix row

1 回表示 (過去 30 日間)
oufouanas
oufouanas 2019 年 1 月 20 日
回答済み: Walter Roberson 2019 年 1 月 20 日
Hi,
I'm trying to write a program in order to store a vector output in a matrix like this:
for k=1:K % number of iterations
for i=1:Sg(i) % Sg is a vector line of K samples
index(i,:)= randsample(1:length(matFragt), Sg(i));% matFragt is a KxK matrix
end
end
What i want is to randomly select (Sg(i)) samples in the matFragt row matrix and store each iteration in a matrix. this proposition gives an error:
" Subscripted assignment dimension mismatch."
I tried to create a cell but it gives me false result
Can any one help me please.
  3 件のコメント
oufouanas
oufouanas 2019 年 1 月 20 日
thank you for your reply, yes Sg values are not the same. I tried to create a cell
B = cell(1, K);
for n=1:K
for i=1:Sg(i)
B{n} = randsample(1:length(matFragt), Sg(i));
end
end
but in each element of the cell is not egal to Sg(i) and i don't get K iterations.
Walter Roberson
Walter Roberson 2019 年 1 月 20 日
You have a problem in your limit in your for i loop: you have the upper bound as Sg(i) but i has not yet been assigned a value.
It seems strange to me that the number of times you want to generate random samples just happens to be the same as the number of random samples you want to generate.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 1 月 20 日
nFrag = length(matFragt);
nSG = length(Sg);
B = cell(K, nSG);
for n = 1 : K
for i = 1 : nSG
B{n, i} = randperm(nFrag, Sg(i));
end
end

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by