Creating Variables in Matlab with "k" and Array of Numbers

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

Discussion Overview

The discussion revolves around creating variables in MATLAB using a combination of a letter and an array of numbers, specifically focusing on the syntax and methods for achieving this. It also touches on issues related to angle calculations in MATLAB, particularly the use of trigonometric functions.

Discussion Character

  • Homework-related, Technical explanation

Main Points Raised

  • One participant seeks help with the correct syntax in MATLAB to create variables like k1, k2, k3 from an array of numbers, aiming for a specific matrix structure.
  • Another participant suggests using sprintf and eval as potential solutions for creating dynamic variable names.
  • A follow-up question is raised about simplifying the process of variable creation in MATLAB, indicating some uncertainty about the efficiency of the suggested methods.
  • Another participant discusses issues with angle calculations, specifically needing to evaluate sin(alpha) correctly to avoid incorrect outputs in their program.
  • A suggestion is made to use atan2 instead of atan to resolve the angle calculation issue.
  • The original poster expresses gratitude for the solution provided regarding angle calculations, indicating it was effective.

Areas of Agreement / Disagreement

Participants generally agree on the utility of using atan2 for angle calculations, but there is no consensus on the best method for creating dynamic variable names in MATLAB, as different approaches are suggested and discussed.

Contextual Notes

There are limitations regarding the clarity of the syntax for dynamic variable creation in MATLAB, and the discussion does not resolve whether the suggested methods are the most efficient or straightforward. Additionally, the angle calculation issue highlights potential misunderstandings in trigonometric function usage.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those working on homework or projects involving variable creation and trigonometric calculations.

elbarto
Messages
32
Reaction score
0

Homework Statement


Hi,
Just wondering if someone could please steer me in the right dirrection for the correct syntax in matlab. I am trying to create a variable from an array of numbers and combine it with the letter k, to give me k1=[] k2=[] k3=[] etc. where [] is a 3x3 matracie




Homework Equations





The Attempt at a Solution



This isn't the problem I am working on but the solution to this would be a great help.

a=[1 3];
['k' int2str(a(2))]=[0 1 2;1 2 3;0 1 2]
? Error: An array for multiple LHS assignment cannot contain character string.



Trying to get this

k3 =

0 1 2
1 2 3
0 1 2


Thanks
Elbarto
 
Physics news on Phys.org
Use sprintf and eval.

- Warren
 
Thanks for the reply, I did get the function to work, but I did read on the net that this was a long winded way of going through the process. does MATLAB have any operations to make this simpler?
 
Just one more question regarding matlab, I am having trouble working with angles. I think sin(alpha) needs to be -1 {or I might just be haveing a really bad day with my maths}. Either way, can someone please explain what I need to do to get pi() instead of 0. I wrote a program the works off inputted co-odinates and it is not operating like it should for horrizontal member.
consider moveing from point (0,0) to (-1,0)

>> dx=-1-0

dx =

-1

>> dy=0- 0

dy =

0

>> alpha=atan(dy/dx)

alpha =

0

>> sin(alpha)

ans =

0

For the rest of my program to work, I need to be able to evaluate sin properly otherwise the output is useless.

This is probably an easy question and the solution might be quite obvious but with my limited experience I haven't managed to think of anything yet.

Thank You
Regards Elbarto
 
Use atan2 instead of atan.
 
Thanks allot for the reply. Works a treat, just what I was looking for.

Regards Elbarto
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K