Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have a mixed-type matrix in Matlab...and how?

I'm trying to write a function with a matrix (specifically a matrix) as the output, the rows of which show a double-type variable and a binary 'status'. For no real reason and just out of curiosity, I wonder if there is a way to have the rows to have different types.

Many Thanks

like image 745
arsaKasra Avatar asked Sep 03 '25 05:09

arsaKasra


1 Answers

MATLAB offers two viable options for storing mixed data types:

Cell arrays

You can look at a cell array as a special matrix where and each element (called cell) can be of a different type (and size). For instance:

C = {2, 'Hello'}

is a cell array that stores both a double and a string.

Structures

Structures can also store values of different data types and sizes, each in a different field. For example, the information in the cell array above can be represented as a structure in the following way:

S.count = 2
S.name = 'Hello'


Recommendation:
It seems that a struct looks more elegant for your needs; I suppose you'll have a field called status storing a boolean value and a field called number storing a double. For multiple values you can store a vector of booleans in status and a vector of doubles in number. Alternatively you can have an array of structs holding one boolean and one double each.

like image 150
Eitan T Avatar answered Sep 05 '25 01:09

Eitan T