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
Click For Summary

Discussion Overview

The discussion revolves around creating a two-column matrix in Matlab using modular operations. Participants explore various methods and approaches to achieve this, including the use of vectors, modular arithmetic, and integer division.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Some participants suggest creating a vector of numbers (0:100) and applying the modulo 10 operation to generate a repeating cycle of digits (0-9).
  • Others propose using integer division to separate the tens' digits from the original numbers.
  • A participant provides pseudocode to illustrate the logic behind filling a two-dimensional array, noting that Matlab has similar functions like mod() and idivide().
  • One participant shares a specific Matlab code snippet that successfully produces the desired matrix format.
  • Another participant expresses appreciation for the assistance received in the discussion.
  • There is a comment comparing Matlab to other programming languages, specifically mentioning APL for its expressiveness with arrays.

Areas of Agreement / Disagreement

Participants present multiple approaches and methods without reaching a consensus on a single solution. Various viewpoints and techniques remain in discussion.

Contextual Notes

Some participants reference specific functions in Matlab, but there is no agreement on the best method to implement the solution. The discussion includes both code examples and theoretical explanations, with some uncertainty about the exact syntax and functions available in Matlab.

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.
 
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: 471
  • 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

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
Replies
0
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
10
Views
3K