Matlab arrays with numbers and characters

In summary, to create an array with both numbers and specific characters displayed in a standard format, you can use a cell array in Matlab. However, you cannot store different data types in the same matrix in the way you want. Refer to Matlab's documentation on cell arrays for more information on how to use them.
  • #1
kaos4
4
0
So, I'm trying to create an array with both numbers and specific characters, like # and C. I want it to be displayed like a standard array of numbers. So the output would resemble.

7 7 7 7 7 7 7
6 6 6 6 6 6 6
5 # C 6 6 @ 6

Thanks,
 
Physics news on Phys.org
  • #2
You can't store different data types in the same matrix in Matlab in the manner in which you want. You can, however, use a cell array to do what you want. For instance, to create a 2x2 cell array with a mixture of double and chars one would type

Code:
> C = {1 'a'; 'b' 21}

C = 

    [1]    'a' 
    'b'    [21]

Check Matlab's documentation for *loads* of info on how to use cell arrays.
 
  • #3
for your question! It is possible to create an array in Matlab that contains both numbers and characters. The key is to use the char() function to convert the characters into a numerical representation that can be stored in the array. For example, the character '#' has a numerical value of 35 and the character 'C' has a numerical value of 67. So, your array would look something like this:

7 7 7 7 7 7 7
6 6 6 6 6 6 6
5 35 67 6 6 64 6

To display the array in a standard format, you can use the disp() function. For example, the code would look like this:

array = [7 7 7 7 7 7 7; 6 6 6 6 6 6 6; 5 35 67 6 6 64 6];
disp(array);

This would give you the desired output:

7 7 7 7 7 7 7
6 6 6 6 6 6 6
5 # C 6 6 @ 6

I hope this helps! Let me know if you have any further questions.
 

What is an array in Matlab?

An array in Matlab is a data structure that can store a collection of values, such as numbers and characters, in a single variable. It is a fundamental concept in Matlab and is used to perform various mathematical and statistical operations.

How do I create an array in Matlab?

To create an array in Matlab, you can use the square brackets notation, where the values are separated by commas or spaces. For example, myArray = [1, 2, 3, 4, 5] or myArray = [1 2 3 4 5] are both valid ways to create an array with the values 1, 2, 3, 4, and 5.

Can an array in Matlab contain both numbers and characters?

Yes, an array in Matlab can contain both numbers and characters. This is one of the key features of arrays in Matlab, allowing for easy manipulation and analysis of different types of data in the same variable.

How do I access elements in an array in Matlab?

To access elements in an array in Matlab, you can use indexing. Indexing starts at 1, so the first element in an array can be accessed using myArray(1). You can also access multiple elements at once using a range, such as myArray(2:4) to access elements 2, 3, and 4.

Can I change the size of an array in Matlab?

Yes, you can change the size of an array in Matlab using the reshape function or by indexing. For example, you can use myArray = reshape(myArray, 3, 2) to change a 1x6 array into a 3x2 array. You can also use indexing to add or remove elements from an array.

Similar threads

Replies
0
Views
311
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Precalculus Mathematics Homework Help
Replies
13
Views
908
  • Precalculus Mathematics Homework Help
Replies
4
Views
742
  • Nuclear Engineering
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • Introductory Physics Homework Help
Replies
7
Views
850
  • Linear and Abstract Algebra
Replies
7
Views
928
Back
Top