Understand a command line to create a matrix
3 views (last 30 days)
I am trying to write the following matlab line into another program, I already found it in the internet, it works perfectly, but I can not understand excatly how it work ??
clc; nelments= 3; npoint= 5;
connect=(1:npoint) + (npoint-1)*(0:nelments-1).'
Geoff Hayes on 8 Oct 2021
@Mark Sc - this is an interesting piece of code. Given that
>> x = (1:npoint)
1 2 3 4 5
>> y = (npoint-1)*(0:nelments-1).'
we are trying to add a 1x5 matrix with a 3x1 matrix with the output being a 3x5. Looking at the plus for 2-D inputs, if one input is a column vector, and the other is a row vector then your output (in this case) will be that 3x5 matrix where we add 0 to each element of x to get the first row of the new matrix, 4 to each element of x to get the second row of the new matrix, and 8 to each element of x to get the third row.
>> x + y
1 2 3 4 5
5 6 7 8 9
9 10 11 12 13