Generate Matrix with MATLAB - 65 Characters

In summary: I used to use Mathcad from version 3, but since 2000, it's been just a read-only viewer. What I do to check out any matrix operations is to build a test problem in Excel, one with lots of values, like 5 or 10 rows and columns, and then do the same in Mathcad, or Maple or J. I then use the results to write a little function or procedure in my little language.
  • #1
DryRun
Gold Member
838
4
I have attached the problem to this post. My attempt at the first part (i used 'm' instead of 'l' as it's less confusing, since the latter resembles the digit '1') and here is my script:

Code:
A = zeros(10);
for k=1:10 
  for  m=1:10
    A(k,m)  =  sin(k)*cos(m); 
  end 
end 
 
A

The answer:

A =

0.4546 -0.3502 -0.8330 -0.5500 0.2387 0.8080 0.6344 -0.1224 -0.7667 -0.7061
0.4913 -0.3784 -0.9002 -0.5944 0.2579 0.8731 0.6855 -0.1323 -0.8285 -0.7630
0.0762 -0.0587 -0.1397 -0.0922 0.0400 0.1355 0.1064 -0.0205 -0.1286 -0.1184
-0.4089 0.3149 0.7492 0.4947 -0.2147 -0.7267 -0.5706 0.1101 0.6895 0.6350
-0.5181 0.3991 0.9493 0.6268 -0.2720 -0.9207 -0.7229 0.1395 0.8737 0.8046
-0.1510 0.1163 0.2766 0.1826 -0.0793 -0.2683 -0.2107 0.0407 0.2546 0.2344
0.3550 -0.2734 -0.6504 -0.4294 0.1864 0.6308 0.4953 -0.0956 -0.5986 -0.5513
0.5346 -0.4117 -0.9795 -0.6467 0.2806 0.9500 0.7459 -0.1440 -0.9014 -0.8301
0.2227 -0.1715 -0.4080 -0.2694 0.1169 0.3957 0.3107 -0.0600 -0.3755 -0.3458
-0.2939 0.2264 0.5386 0.3556 -0.1543 -0.5224 -0.4101 0.0792 0.4957 0.4565

which i hope is correct?

But i have no idea how to do the same thing with a loop-free script.
 

Attachments

  • matrix.PNG
    matrix.PNG
    12.4 KB · Views: 527
Last edited:
Physics news on Phys.org
  • #2
Here is my attempt for part 2 but i think there is a shorter version:

k=sin(1:1:10);
m = cos(1:1:10);
r1=k(1).*m
r2=k(2).*m
r3=k(3).*m
r4=k(4).*m
r5=k(5).*m
r6=k(6).*m
r7=k(7).*m
r8=k(8).*m
r9=k(9).*m
r10=k(10).*m
A=[r1;r2;r3;r4;r5;r6;r7;r8;r9;r10]
 
  • #3
sharks said:
I have attached the problem to this post. My attempt at the first part (i used 'm' instead of 'l' as it's less confusing, since the latter resembles the digit '1') and here is my script:

Code:
A = zeros(10);
for k=1:10 
  for  m=1:10
    A(k,m)  =  sin(k)*cos(m); 
  end 
end 
 
A


The answer:

A =
0.4546 ...

which i hope is correct?

Well, they seem to agree with my Mathcad-generated results, if that's of some reassurance.

But i have no idea how to do the same thing with a loop-free script.


Try something like:
k = 1:10;
A = sin(k)' * cos(k);

note the single quote mark, the transpose operator. The creates a vector, k, with values 1 to 10, the sin and cos functions operate over vectors, resulting in 2 vectors, the sin is transposed and the resulting matrix multiplication is equivalent to the first loop method.

You can use just k as the number of k and m elements are the same.
 

Attachments

  • phys - 12 06 15 matlab 2D multiplication 01.jpg
    phys - 12 06 15 matlab 2D multiplication 01.jpg
    32.4 KB · Views: 665
  • #4
Forgot to add, image showing proof of pudding in Mathcad.
 

Attachments

  • phys - 12 06 15 matlab 2D multiplication 02.jpg
    phys - 12 06 15 matlab 2D multiplication 02.jpg
    32.5 KB · Views: 631
  • #5
NemoReally said:
Try something like:
k = 1:10;
A = sin(k)' * cos(k);

note the single quote mark, the transpose operator. The creates a vector, k, with values 1 to 10, the sin and cos functions operate over vectors, resulting in 2 vectors, the sin is transposed and the resulting matrix multiplication is equivalent to the first loop method.

You can use just k as the number of k and m elements are the same.

That's an ingenious way of solving it, as a 10x1 matrix multiply by another 1x10 matrix gives the required 10x10 matrix! :smile:

Thank you very much, NemoReally.
 
  • #6
sharks said:
That's an ingenious way of solving it, as a 10x1 matrix multiply by another 1x10 matrix gives the required 10x10 matrix! :smile:
It is, isn't it. :smile:


The important thing about languages such as Mathcad, Mathematica, Maple, Matlab and J is learning to get one's head out of the detailed programming gutter and looking up into the higher level world of mathematics, particularly arrays and functions. There are still occasions when a bit of close quarters coding is required, but its far better to be able to think about multiplying two matrices at company commander level rather than march them around the binary parade square yourself. (I now it declare it International Mixed Metaphor Day.) Imagine you're writing the problem down on a whiteboard and then see what methods exist to support what you've written.

Thank you very much, NemoReally.
No worries.
 

What is a matrix in MATLAB?

A matrix in MATLAB is a two-dimensional array of numbers or variables, arranged in rows and columns. It is a fundamental data structure used in many scientific and engineering applications.

How do I generate a matrix with 65 characters in MATLAB?

To generate a matrix with 65 characters in MATLAB, you can use the "randi" function and specify the size of the matrix as 65 characters. For example, you can use the code "A = randi([65,90], 1, 65)" to generate a matrix with 65 random characters between A-Z.

Can I generate a matrix with non-numeric characters in MATLAB?

Yes, it is possible to generate a matrix with non-numeric characters in MATLAB. You can use the "char" function to convert the numeric values into characters. For example, you can use the code "A = char(randi([65,90], 1, 65))" to generate a matrix with 65 random characters between A-Z.

What is the difference between a matrix and a cell array in MATLAB?

A matrix in MATLAB can only hold numeric values, while a cell array can hold different data types, including strings, characters, and even other matrices. Cell arrays are more flexible, but matrices are more efficient for numerical computations.

Can I resize a matrix in MATLAB?

Yes, you can resize a matrix in MATLAB using the "reshape" function. This function allows you to change the dimensions of a matrix without changing its data. You can specify the desired size of the matrix, and MATLAB will automatically rearrange the elements to fit the new dimensions.

Back
Top