MATLAB How can I create a matrix with two columns in Matlab using a modular operation?

  • Thread starter Thread starter confused_engineer
  • Start date Start date
  • Tags Tags
    Matlab Program
AI Thread Summary
The discussion centers on creating a two-row matrix in MATLAB, where the first row contains a repeating cycle of digits from 0 to 9 and the second row contains the corresponding tens digits. Participants suggest using MATLAB functions like `mod()` for the modulus operation and `fix()` or `idivide()` for integer division to achieve this. A sample MATLAB code snippet is provided, demonstrating how to generate the desired matrix by creating a vector from 0 to 20, applying the modulus and division operations, and arranging the results into two rows. The conversation highlights the utility of MATLAB for matrix manipulation and array operations.
confused_engineer
Messages
34
Reaction score
1
Hello everyone.
I am trying to create a matrix in Matlab with two columns which should look like as follow:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9...
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1...
Has anyone programed something like this before?
 
Physics news on Phys.org
confused_engineer said:
Hello everyone.
I am trying to create a matrix in Matlab with two columns which should look like as follow:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9...
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1...
Has anyone programed something like this before?
That would be two rows, as written. Does this help?

https://www.mathworks.com/help/matlab/learn_matlab/matrices-and-arrays.html
 
  • Like
Likes confused_engineer
You could create a vector of numbers say 0:100
And then make a new vector of numbers using the module 10 operation or functional give you the 0:9 cycle.

Similarly you could integer divide each number in the first array and then integer multiply by 10

Matlab may have a function to do that something like floor.
 
  • Like
Likes confused_engineer
The following isn't MATLAB code, but it might serve as pseudocode that can help you out.
C:
int arr[2][100];  // Arrays are declared differently in matlab
// Fill the first row (arr[0][ ... ]) with the repeating cycle of digits 0, 1, 2, ..., 9
// Fill the second row (arr[1][ ...]) with the tens' digits.
for (int j = 0; j< 100; j++)
{
   arr[0][j] = j % 10;
   arr[1][j] = j / 10;
}
The C expression j % 10 evaluates to the remainder when j is divided by 10. For example, 3 % 10 is 3, and 25 % 10 is 5. In matlab, there is a mod() function. mod(18, 10) would evaluate to 8.
The C expression j / 10 uses the feature in C and languages based on C of integer division, which is distinct from floating point division. For example 18 /10 is 1 (not 1.8). In matlab, I believe that idivide(j, 10) would be the MATLAB equivalent. (I don't have matlab.)
 
  • Like
Likes FactChecker, confused_engineer and jedishrfu
I think that I have it.

Thanks for the help, you have helped me a big deal.
 
Matlab is an interesting tool. Cool but not quite as revolutionary as IBM’s APL for expressiveness in working with arrays of numbers.
 
This produces your array in MATLAB.

UIGDZJP.png
 

Attachments

  • UIGDZJP.png
    UIGDZJP.png
    4.2 KB · Views: 434
  • Like
Likes FactChecker and jedishrfu
jedishrfu said:
You could create a vector of numbers say 0:100
And then make a new vector of numbers using the module 10 operation or functional give you the 0:9 cycle.

Similarly you could integer divide each number in the first array and then integer multiply by 10

Matlab may have a function to do that something like floor.

One fairly straightforward Matlab-style approach is:
Code:
>> A = 0:20

A =

     0     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18    19    20

>> A = [mod(A,10); fix(A/10)]

A =

     0     1     2     3     4     5     6     7     8     9     0     1     2     3     4     5     6     7     8     9     0
     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     2
 
  • Like
Likes FactChecker
Back
Top