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

  • Context: MATLAB 
  • Thread starter Thread starter confused_engineer
  • Start date Start date
  • Tags Tags
    Matlab Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 2K views
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   Reactions: 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   Reactions: 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   Reactions: FactChecker, confused_engineer and jedishrfu
I think that I have it.

Thanks for the help, you have helped me a big deal.
 
This produces your array in MATLAB.

UIGDZJP.png
 

Attachments

  • UIGDZJP.png
    UIGDZJP.png
    4.2 KB · Views: 484
  • Like
Likes   Reactions: 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   Reactions: FactChecker