How to create this matrix not using loops and if/else statements?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello. I have a problem with creating matrix like this without using loops and if/else statements. It must have 5 columns and N rows.
0 1 1 1 0/(N-1)
0 1 1 2 1/(N-1)
. . . 3 2/(N-1)
. . . . .
0 1 1 N (N-1)/(N-1)
I ended up with this:
A1 = [0 1 1 N (N-1)/(N-1)];
A2 = reshape(A1, [], N);
A = repmat(A2, N, 1)
----------------------------
(N = 5)
ans =
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1
0 1 1 5 1
0 件のコメント
採用された回答
Can Atalay
2021 年 10 月 17 日
編集済み: Can Atalay
2021 年 10 月 17 日
number_of_columns = 5;
number_of_rows = 10; % you can change this to N
first_column = zeros(number_of_rows,1);
second_and_third_columns = ones(number_of_rows,2);
fourth_column = transpose(1:number_of_rows);
fifth_column = transpose(0:(number_of_rows-1))/(number_of_rows-1);
end_result = [first_column second_and_third_columns fourth_column fifth_column]
% end_result = [first_column, second_and_third_columns, fourth_column, fifth_column]
% the line above would also work
Did I get the question right? This should work if you change the second line's value from 10 to N
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!