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

  • MATLAB
  • Thread starter confused_engineer
  • Start date
  • Tags
    Matlab Program
In summary: 2 2 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 9 9
  • #1
confused_engineer
39
2
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
  • #2
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
  • #3
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
  • #4
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
  • #5
I think that I have it.

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

UIGDZJP.png
 

Attachments

  • UIGDZJP.png
    UIGDZJP.png
    4.2 KB · Views: 363
  • Like
Likes FactChecker and jedishrfu
  • #8
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

1. How do I define a variable in Matlab?

In Matlab, you can define a variable by using the assignment operator (=) followed by the variable name and the value you want to assign. For example, to define a variable "x" with the value 5, you would type:
x = 5

2. How do I create a loop in Matlab?

To create a loop in Matlab, you can use the for or while statement. For example, to create a for loop that executes 10 times, you would type:
for i = 1:10
% loop body
end

3. How do I read data from a file in Matlab?

You can read data from a file in Matlab using the load function or the fopen and fscanf functions. The load function is used for reading data from .mat files, while fopen and fscanf functions are used for reading data from text files.

4. How can I plot data in Matlab?

To plot data in Matlab, you can use the plot function. This function takes two vectors as input, one for the x-axis values and one for the y-axis values. For example, to plot a line graph of x and y values, you would type:
plot(x,y)

5. How do I define a function in Matlab?

You can define a function in Matlab using the function keyword, followed by the function name, input arguments, and the function body. For example, to define a function that calculates the area of a circle, you would type:
function area = circle_area(radius)
area = pi*radius^2;
end

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top