46785

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

Question:

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

Answer1:

MATLAB offers two viable options for storing mixed data types:

<h3>Cell arrays</h3>

You can look at a <a href="http://www.mathworks.com/help/matlab/cell-arrays.html" rel="nofollow">cell array</a> as a special matrix where and each element (called <em>cell</em>) 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.

<h3>Structures</h3>

<a href="http://www.mathworks.com/help/matlab/structures.html" rel="nofollow">Structures</a> can also store values of different data types and sizes, each in a different <em>field</em>. 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'

<br /> Recommendation:<br /> 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.

Recommend

  • NetworkCredential.Domain Property
  • Is there any way to distinguish between an unset property and a property set to undefined? [duplicat
  • Correspondece between Git client and server versions
  • filter/remove invalid xml characters from stream
  • How do I write a lambda expression that looks like a method?
  • How to make a matlab legend recognize multiple scatter plots?
  • Date api for managing off days in an year?
  • Excel not inserting leading zero
  • Error building MEX-files (MATLAB 2012a) on Kubuntu '/usr/bin/ld: cannot find -lstdc++'
  • Override bootstrap style not working
  • True privateness in Python
  • integration of function in modelica
  • Python function to read variable length blocks of data from file while open
  • Slicing an SPA into several components and use AngularJS
  • Creating a C++ function that calls other Lua function
  • MVC - @Html.CheckBoxFor
  • Why isn't my “Fizz Buzz” test in R working?
  • What is the difference between a “service account” and an “installed application”?
  • Validate jQuery plugin, field not required
  • Cast between interfaces whose interface signatures are same
  • Center align outputs in ipython notebook
  • custom UITableViewCell with image for highlighting
  • Create DicomImage from scratch using Dcmtk
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • C++ Partial template specialization - design simplification
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Projection media query: browser support and workarounds?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Javascript simulate pressing enter in input box
  • Deleting and Updating values from a cusrsor adapter
  • Modifying destination and filename of gulp-svg-sprite
  • Sending data from AppleScript to FileMaker records
  • vba code to select only visible cells in specific column except heading
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check