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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Can Atalay
Can Atalay el 17 de Oct. de 2021
Editada: Can Atalay el 17 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre MATLAB Compiler en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by