Filter löschen
Filter löschen

How to create this matrix not using loops and if/else statements?

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Can Atalay
Can Atalay am 17 Okt. 2021
Bearbeitet: Can Atalay am 17 Okt. 2021
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

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by