Matlab: how to use an array in a matrix

In summary, the conversation involved the user, Elham, seeking help with creating a 2x2 matrix in MATLAB. They shared their code, which resulted in a 2x2x100 array instead of a plain 2x2 matrix. After some back and forth, it was determined that the user wanted to replace the array elements in the matrix and use it as a transfer matrix. However, this was not possible with the selected array, so they needed a square matrix. In the end, the user solved the problem by using a FOR loop.
  • #1
elham.n
5
0
hi all..

I have a 2x2 matrix and an array , for example:

a=linspace(0,12) %this is my frequency range.

B=[2*a 3*a-1
a./2 a]

this one does not results a 2x2 matrix, that I need it to have ...B2xB1xB
how could i get a 2x2 matrix? should i use a loop instead of an array?
tnx alot.

Elham
 
Physics news on Phys.org
  • #2
What are you trying to accomplish here, elham?
 
  • #3
With what?

What you have here a 2x2x100 array -- which is exactly what you asked for. You just don't know that that is what you asked for.

What you want is something different, apparently a plain old vanilla 2x2 matrix. I don't know how to read your mind, let alone a dumb program like Matlab. It only does what you tell it to do. Matlab, like all other languages built to date, does not have a DWIM (do what I mean) command.
 
  • #4
Matlab did exactly what you told it to do. That apparently is not what you want it to do.

So what exactly are you trying to accomplish?
 
  • #5
I know what u mean!and I do not expect MATLAB to do s.th strange..
I want to replace this array's elements in this matrix.
i will use a matrix like this as the transfer matrix in PC structures,and multiplicantion of these matrices for every layer,but with this selected array , i face some problems. and multiplicantion of these matrices is impossible.
so i need square matrix..and I was only confused
tnx alot..I solved this problem with a FOR loop.
thank u.
 

1. What is an array in Matlab?

An array in Matlab is a data structure that can hold multiple elements of the same data type. It is a type of matrix that can have either a single row or a single column. Arrays are commonly used in Matlab for storing and manipulating data.

2. How do I create an array in Matlab?

To create an array in Matlab, you can use the square brackets notation and separate the elements with commas. For example, to create an array with the numbers 1, 2, and 3, you would write [1, 2, 3]. You can also use the colon operator to create a sequence of numbers, such as 1:5 to create an array with numbers from 1 to 5.

3. How can I access elements in an array in Matlab?

You can access elements in an array by using their index number. In Matlab, arrays are indexed starting from 1. So, to access the first element in an array, you would use the index 1. You can also access a range of elements by using the colon operator, such as array(2:5) to access elements 2 to 5.

4. Can I perform mathematical operations on arrays in Matlab?

Yes, you can perform various mathematical operations on arrays in Matlab. For example, you can add, subtract, multiply, or divide arrays with the same dimensions. You can also perform element-wise operations, where each element in one array is operated on with the corresponding element in another array.

5. How do I convert an array into a matrix in Matlab?

To convert an array into a matrix in Matlab, you can use the reshape function. This function allows you to specify the number of rows and columns in the resulting matrix. If the array has a different number of elements than the specified number of rows and columns, an error will occur.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • General Math
Replies
7
Views
844
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
753
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
Back
Top