フィルターのクリア

How to repeat number of array by values in another matrix

2 ビュー (過去 30 日間)
Hamdia Mansour
Hamdia Mansour 2020 年 6 月 25 日
コメント済み: Hamdia Mansour 2020 年 6 月 25 日
Hello everybody I have an array like array=[3 4 2 1] and I want to create another array which contain repetition of values in this array by no of values in matrix. Matrix =[0 2 4 5; 1 3 2 6; 1 0 3 5; 5 6 3 1] Here for this array first no is 3 this mean repetition in matrix found in first column and row 3 which equal 1 and the second number is 4 also means that second column and forth row. so the resulted array will be 3 4 4 4 4 4 4 2 2 1 1 1 1 1 if someone could help me

採用された回答

Johannes Fischer
Johannes Fischer 2020 年 6 月 25 日
array = [3 4 2 1];
matrix = [0 2 4 5; 1 3 2 6; 1 0 3 5; 5 6 3 1];
% first, you need a vector with the same length of array that tells you
% how often each element must be repeated
reps = diag(matrix(array, :));
% now you can use 'repelem' with that vector
result = repelem(array, reps);
  2 件のコメント
Stephen23
Stephen23 2020 年 6 月 25 日
+1 neat solution
Hamdia Mansour
Hamdia Mansour 2020 年 6 月 25 日
Thank you very much Johannes fischer. You save my day. Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by