Generate Matrix with MATLAB - 65 Characters

  • Context: MATLAB 
  • Thread starter Thread starter DryRun
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around generating a matrix using MATLAB, specifically focusing on two approaches: a loop-based method and a loop-free method. Participants explore the implementation details and seek a more efficient solution for the matrix generation problem.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant shares a loop-based script to generate a 10x10 matrix using sine and cosine functions, expressing uncertainty about its correctness.
  • Another participant attempts a loop-free version but presents it in a lengthy manner, suggesting there might be a more concise approach.
  • A later reply proposes a more efficient loop-free method using matrix multiplication, highlighting the use of the transpose operator.
  • Some participants express appreciation for the proposed loop-free method, emphasizing the importance of higher-level mathematical thinking in programming.

Areas of Agreement / Disagreement

Participants generally agree on the validity of the loop-based method but express uncertainty regarding the correctness of the loop-free approach until it is validated. There is no consensus on the best method, as multiple approaches are discussed.

Contextual Notes

Some participants mention the importance of understanding the underlying mathematical concepts when using programming languages like MATLAB, suggesting that higher-level thinking can lead to more efficient solutions.

Who May Find This Useful

Readers interested in MATLAB programming, matrix operations, and efficient coding techniques in mathematical computing may find this discussion beneficial.

DryRun
Gold Member
Messages
837
Reaction score
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: 618
Last edited:
Physics news on Phys.org
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]
 
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: 736
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: 708
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.
 
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.