How to access array/cell element in a uniform way - matlab

In MATLAB, the i-th element of an array is accessed by a(i), while the i-th element of a cell is accessed by a{i}. So my code has to do different things in terms of whether a is a cell or not.
Is there a better way to do it? so we can access the i-th element in a 'same'? way.

You can access the ith element of any array in the same way, a(i), and the behaviour is completely uniform: you get back an object of the same class as a. So if a is a 5x5 double array, a(i) is a 1x1 double. Similarly, if a is a cell array, then a(i) is a 1x1 cell. So far, so logical and consistent.
Now, if you want to look inside a cell, you use a{i} but that is a fundamentally different operation. Your question seems to imply that it would be "better" if these two fundamentally different operations had the same syntax. It wouldn't: if this were the case, then the ability to slice a cell array into arbitrary shapes (including 1x1) would be overshadowed.
But you can always write a custom function. Among the many Matlab workarounds I carry with me everywhere, I have the following pair of functions:
function a = ascell(a)
if ~iscell(a), a = {a}; end
and
function a = uncell(a)
if iscell(a) & numel(a) == 1, a = a{1}; end
With uncell.m on your path, b = uncell(a(i)); would give you the ith element of a with the cell wrapping, if any, stripped off.
It is good to have the call to uncell visible in the code because it alerts you (or another maintainer) to the possibility that a might legally be a cell or a non-cell—this is by no means necessarily true in everybody's coding strategy. Nor will my code necessarily follow the same convention as yours when it comes to interpreting the meaning and correct treatment of a cell array where a non-cell was expected, or a non-cell where a cell was expected (and this is another way of explaining why there's no common syntax). This leads me to the question: if the design of your application is such that a can by its nature contain elements with mismatched shapes or types, then why not simply decree that it is always a cell, never a non-cell, and always access the ith element as a{i}?

Related

Does MATLAB have any set-like datatype?

I am looking for a way to compare finite sequential data with non-deterministic ordering in MATLAB. Basically, what I want is an array, but without imposing an order on the contained elements. If I have the objects
a = [x y z];
and
b = [x z y];
I'd want isequal(a, b) to return true. With arrays, this is not the case. The easy fix would be to sort the entries before comparing them. Unfortunately, in my case the elements are complex objects which cannot easily be mapped to have an unambigious numerical relationship to each other. Another approach would be not to use isequal, but rather a custom comparison function which asserts matching lengths and then simply checks if each element from the first array is contained in the second one. However, in my case the arrays are non-trivially nested inside the structs I am trying to compare via isequal, and it would be quite complicated to write a custom comparison function for the encapsulating structs. Other than this ordering problem, the inbuilt isequal function covers all of my needs, as it correctly handles arbitrarily nested structs with arbitrary fields, so I would really like to avoid writing a complicated custom function for that.
Is there any datatype in MATLAB which allows for the described behavior? Or is there a way to easily build such a custom type? In Java, I could simply write a wrapper class with a custom implementation for the equals method, but there seems to be no such mechanism in MATLAB?
I've found a way to solve my problem elegantly. Contrary to my previously stated belief, MATLAB actually does allow for class-specific overriding of isequal.
classdef CustomType
properties
value
end
methods
function self = CustomType(value)
self.value = value;
end
function equal = isequal(self, other)
if not(isa(other, 'CustomType'))
equal = false;
return;
end
% implement custom comparison rules here
end
end
end
So, I can simply assign the fields in question like this and don't have to change anything else in my code:
a = Set([x y z]); % custom type
...
b = Set([x z y]);
...
isequal(a, b); % true
In my use case, I don't even need the uniqueness property of sets. So I only have to perform order-independent comparison and don't need to waste performance on ensuring unrequired properties. Furthermore, by using a dedicated type, I can differentiate explicitly between fields which have order (i.e. regular arrays) and those which don't, at the moment of assignment.
Another solution might be to overwrite the inbuilt isequal and make it apply custom comparison rules when its arguments are of specific type. However, this would slow down all comparisons in the whole program and make for bad encapsulation. I feel like using a custom type with an overriden isequal is the way to solve this kind of problem. But I still think that sets (and other types of commonly used containers) should be included in the basic repertoire of MATLAB.

MATLAB cell array and array - error

I'd like to understand why the following code works:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b{1}=frame2im(a);
instead the following code does not work:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b(1)=frame2im(a);
If I use "b(1)=x;" it works.
Thank you very much.
In an array, you can store only one 1x1 value of any class at a single index but the class of all elements of an array must be same. In a cell array, there is no such restriction.
frame2im(a) is [525x700x3 uint8] and hence you can store it in a cell and not in a simple array if you want to store it at a single index.
b(1)=x; works because x is 1x1 matlab.ui.Figure. You can also store x in a cell array.
To my understanding, you need to know what cells are meant for in MATLAB. If you happen to know Python, you probably will think in a "list"-type way. MATLAB cell can store numbers, strings, etc. However its array is meant to store numbers.
That's why your structure from fram2im can't work.

"Reverse" of arrayfun

I have an array of "object" structures, OBJECT_ARRAY, that I must often transform into individual arrays for each element of the object structures. This can be done using arrayfun. It's more tedious than simply refereeing to OBJECT_ARRAY(k).item1, but that's how The Mathworks chose to do it.
In this case today, I have used those individual arrays and calculated a corresponding derived value, newItem, for each element and I need to add this to the original array of structures. So I have an array of newItems.
Is there a straightforward way to do an assignment for each object in OBJECT_ARRAY so that (effectively) OBJECT_ARRAY(k).newItem = newItems(k) for every index k?
I am using version 2015a.
You shouldn't need arrayfun for any of this.
To get values out, you can simply rely on the fact that the dot indexing of a non-scalar struct or object yields a comma-separated list. If we surround it with [] it will horizontally concatenate all of the values into an array.
array_of_values = [OBJECT_ARRAY.item1];
Or if they are all different sizes that can't be concatenated, use a cell array
array_of_values = {OBJECT_ARRAY.item1};
To do assignment, you can again use the comma separated list on the left and right side of the assignment. We first stick the new values in a cell array so that we can automatically convert them to a comma-separated list using {:}.
items = num2cell(newitems);
[OBJECT_ARRAY.item1] = items{:};

"some_variable = []" type of statement in matlab

Hello and thanks for the help,
I have seen that you can make this statement:
some_variable = []
from what I see, this makes like a void variable for later use.
The thing is I want to make it an array (and that is the problem). I made this
some_variable(:) = []
but there is an error:
In an assignment A(:) = B, the number of elements in A and B must be the same
Thanks
Actually everything in MATLAB is an array, sometimes the arrays are just 1x1, but they are arrays nevertheless (unlike in C/C++ where you have int or int*).
So, when you do var=[] you initialize an empty array, an array of size 0*.
After that, its up to you to initialize it with whatever size you want. var=0 will initialize it as an array of 1x1 size, but you can go bigger, using zeros(size).
Adittionally, if you want to create empty variables of another class, just use <classname>.empty, such as in var=uint32.empty;
*Note that in MATLAB, an array is also infinite dimensional. It's not size 0, its 0x0x0x0x0x.....x0x0. If you want to prove this, try size(var,999999999999) in MATLAB.

How to populate an array within an array in MATLAB and how to access them?

For example
A = {{1,2},{23,34},{45,4},...}
How to create such a data type in MATLAB
How to access the i-th element and the elements within it?
For Ex. A[2] should return {23,34} and A[2].1 should return 23.
A = {{1,2},{23,34},{45,4},...}
is valid MATLAB syntax, if you are trying to make a cell array of cell arrays. However, you'd probably want to store vectors rather than arrays in your array:
A = {[1,2],[23,34],[45,4],...}
Access them like A{2} or A(2) but far more likely the first one. If you want an individual element then A{2}(1)
But if every one of your cells is going to contain a 2 element vector then you will have a much easier time just using a 2D matrix:
A = [1,2;23,34;45,4;,...]
And and now access the entire row i.e. A(2,:) or for an individual element A(2,1)