creating array based in another array

19 ビュー (過去 30 日間)
Khairul Nur
Khairul Nur 2021 年 8 月 11 日
コメント済み: Khairul Nur 2021 年 8 月 16 日
Hi, i have an array nx1 (refer array 1 as below) and i want to create 2nd array based on the 1st array as follow:
array 1:
1
2
3
4
5
For example:
Loop 1: read 1st index array 1 which is 1, array 2 will be
array 2: 2 3 4 5
Loop 2: read 2nd index array 1 which is 2, array 2 will be
array 2: 1 3 4 5
Loop 3: read 3rd index array 1 which is 3, array 2 will be
array 2: 1 2 4 5
and so forth.
Is it possible without using for loops?

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 11 日
L = length (A);
temp = repmat(A, L, 1);
temp(1:L+1:end) = [] ;
B = reshape(temp,L-1,[]);
  1 件のコメント
Khairul Nur
Khairul Nur 2021 年 8 月 16 日
tq walter :)

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

その他の回答 (1 件)

Fabio Freschi
Fabio Freschi 2021 年 8 月 11 日
Here a possible solution, where all arrays you want are in the columns of matrix a2
% number of entries in array 1
n = 5;
% create array 1
a1 = (1:n).';
% create matrix a2 as column vector
a2 = repmat(a1,n,1)
% remove diagonal entries
a2(1:n+1:n^2) = [];
% reshape a2 to obtain the matrix
a2 = reshape(a2,n-1,n);
The output is
a2 =
2 1 1 1 1
3 3 2 2 2
4 4 4 3 3
5 5 5 5 4
  1 件のコメント
Khairul Nur
Khairul Nur 2021 年 8 月 16 日
hi...ur code also working.. thank u for the code

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by