2

I am trying to implement the Brooks-Iyengar algorithm for sensor fusion, and am trying to represent the following data structure in MATLAB.

A = {([1.5,3.2],4), ([0,2.7],5), ([0.8,2.8],4)}

I tried doing the following

B = {{[1.5,3.2],4},{[0,2.7],5}}

But then I don't know how to access each element, i.e. 1.5, 3.2 and the 4 as well as the next set of values. I get one set of elements from B{1}, but am not able to get the individual values after.

Any ideas or pointers to appropriate links would be helpful.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
GothamCityRises
  • 2,072
  • 2
  • 27
  • 43

3 Answers3

4

With the current structure, you can simply continue the indexing:

>> B{1}

ans = 

    [1x2 double]    [4]

>> B{1}{1}

ans =

    1.5000    3.2000

>> B{1}{1}(2)

ans =

    3.2000

>> B{1}{2}

ans =

     4

To remove an item from the main structure you can use the following syntax B(1) = [];:

>> B = {{[1.5,3.2],4},{[0,2.7],5}}

B = 

    {1x2 cell}    {1x2 cell}

>> B(1) = []

B = 

    {1x2 cell}

>> B{1}

ans = 

    [1x2 double]    [5]

>> 

You could also choose to represent the data in a structure array (with some better property naming):

>> s = struct('prop1',{4, 5},'prop2', {[1.5,3.2], [0,2.7]})

s = 

1x2 struct array with fields:

    prop1
    prop2

>> s(1).prop1

ans =

     4

>> s(1).prop2

ans =

    1.5000    3.2000

>> s(1).prop2(2)

ans =

    3.2000

To remove an item, you can use the similar syntax:

s(1) = []

If you want to performs some operations on the data elements, you can also choose to go with the OOP approach, and create a class that represents a single data element and optionally one that represents the whole data set. Accessing the data members is natural.

DVarga
  • 21,311
  • 6
  • 55
  • 60
  • That looks great. Just a follow up question, how would I remove a data entry (say, `{[1.5,3.2],4}` ) from the first structure? – GothamCityRises Mar 23 '17 at 15:20
  • 1
    Updated the answer about how to remove elements using the original or the structure array approaches. – DVarga Mar 23 '17 at 15:41
  • But when I do B{1} = [], the size of the cell doesn't reduce. It has a an empty entry. Any way to remove that empty set as well? – GothamCityRises Mar 24 '17 at 00:59
  • `B{1} = []` differs from `B(1) = []`. The first one assigns an empty entry, the second one will remove the entry. – DVarga Mar 24 '17 at 06:20
2

If your MATLAB version is new enough (i.e. >= R2013b) you can use a table for this:

A = table([1.5,3.2; 0,2.7; 0.8,2.8],[4; 5; 4],'VariableNames',{'name1','name2'});

enter image description here

As you can see, the result is easy to inspect (visually) and easy to access:

A.name1(3,2) % is 2.8000
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
1

If you are dead set on using cells then I'd start with cell2mat and see if that helps out.

  vals = cell2mat(B{2}) % returns the array vals=[0 2.7 5]

You could also simply use your data as normal matrix from the start:

 B = [ 1.5, 3.2, 4; 0, 2.7, 5];

And then utilize column 3 as your keys if that was your intent (and they are numeric). If the keys are not guaranteed numeric then a struct could be useful.

edit: DVarga gives a more detailed and useful answer, I think.

Morc
  • 381
  • 2
  • 9