Arrays containing text in MATLAB - arrays

In MATLAB, when code like this is implemented:
c = ['a','b','c','d'];
you can't really do anything with the elements. To illustrate my example:
>> c
c =
abcd
and when you do c(1,1) it returns A. But for c(2,1) it returns Index exceeds matrix dimensions.
To combat this problem, is there any way I could get around it? Or perhaps a different type of array?

You need to store each string in a different row, like so:
c = ['a';'b';'c';'d'];
What you have done above is use the [], which is the string concatenation operator. What it outputs is a single string, 'abcd', stored in c(1), which is why c(2) throws an index error.
Alternatively, you could use cell arrays :
c{1} = 'a';
c{2} = 'b';

Related

squares showing upon implicit conversion from numeric to char

I am trying to test the behavior of adding number in an array containing a string using the following code :
warning: implicit conversion from numeric to char
I do understand the error and all, but the weird thing is that I get squares for the numbers, as in the image and I don't understand what these are
If you want to mix heterogeneous input data in an array with Octave, you must use a cell array as container, that is, use braces instead of brackets to "concatenate" data:
>> a6 = {"test", 3, 5}
a6 =
{
[1,1] = test
[1,2] = 3
[1,3] = 5
}
Otherwise, as commented by Raymond Chen, Octave tries to convert some data to make homogeneous (of the same type) all of them.

What is the point of cell indexing in MATLAB

The point of indexing is mainly to get the value. In MATLAB,
for a cell array, there is content indexing ({}), and thus cell indexing (()) is only for selecting a subset from the cell array, right?
Is there anything other advanced usage for it? Like using it as
a pointer and pass it to a function?
There is a heavily simplified answer. {}-indexing returns you the content, ()-indexing creates a subcell with the indexed elements. Let's take a simple example:
>> a=x(2)
a =
[2]
>> class(a)
ans =
cell
>> b=x{2}
b =
2
>> class(b)
ans =
double
Now continue with non-scalar elements. For the ()-indexing everything behaves as expected, you receive a subcell with the elements:
>> a=x(2:3)
a =
[2] [3]
The thing really special to Matlab is using {}-indexing with non-scalar indices. It returns a Comma-Separated List with all the contents. Now what is happening here:
>> b=x{2:3}
b =
2
The Comma-Separated List behaves similar to a function with two return arguments. You want only one value, only one value is assigned. The second value is lost. You can also use this to assign multiple elements to individual lists at once:
>> [a,b]=x{2:3} %old MATLAB versions require deal here
a =
2
b =
3
Now finally to a very powerful use case of comma separated lists. Assume you have some stupid function foo which requires many input arguments. In your code you could write something like:
foo(a,b,c,d,e,f)
Or, assuming you have all parameters stored in a cell:
foo(a{1},a{2},a{3},a{4},a{5},a{6})
Alternatively you can call the function using a comma separated list. Assuming a has 6 elements, this line is fully equivalent to the previous:
foo(a{:}) %The : is a short cut for 1:end, index the first to the last element
The same technique demonstrated here for input arguments can also be used for output arguments.
Regarding your final question about pointers. Matlab does not use pointers and it has no supplement for it (except handle in oop Matlab), but Matlab is very strong in optimizing the memory usage. Especially using Copy-on-write makes it unnecessary to have pointers in most cases. You typically end up with functions like
M=myMatrixOperation(M,parameter,parameter2)
Where you input your data and return it.

Conversion of entire array from int to double in order to do some aritmetic operations

I have this bit of code:
var arrayIntegers : [Int] = []
arrayIntegers += 0...33
var arrayDecimal : [Int] = []
arrayDecimal += 0...999
The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part.
When I try to typecast this:
var arrayDoubleComposed : [Double] = []
arrayDoubleComposed = Double (arrayIntegers[] + (arrayDecimal[])/1000)
I´ve got an error. The same if I suppress the [].
I´m a little bit newcomer, I know...
Converting one kind of array into another is a job for map, which applies a function to each element of an array, returning the results as a new array of the type that function returns. In this case, you want a function that converts the Ints to a Double.
Try something like this:
let integers = Array(0...33)
let fractions = Array(0...999)
let arrayDoubleComposed = map(Zip2(integers, fractions)) {
(i, f) in
Double(i) + Double(f)/1_000
}
Zip2 takes two sequences and pairs them up – first elements together, second elements together etc. Then this passes that into map which combines the two elements.
(note also you can just initialize the arrays from the ranges rather than declaring them then adding the values)
It’s not clear what you mean to do, as the integer and fractional arrays are going to be different length. The way Zip2 handles this is to stop when the first sequence runs out, but this may not be what you want.
P.S. casts like the one you tried, which convert the contents of an array en-mass, only work in special cases when converting from Objective-C types to native Swift types, when the Swift compiler sprinkles some magic.

Is there an idiomatic way to exchange two elements in a cell array?

I know that I can write it like this:
tmp = arr{i}
arr{i} = arr{j}
arr{j} = tmp
But is there a simpler way? For instance, in Python I'd write:
arr[i], arr[j] = arr[j], arr[i]
Standard, idiomatic way:
Use a vector of indices:
arr([i j]) = arr([j i]); %// arr can be any array type
This works whether arr is a cell array, a numerical array or a string (char array).
Not recommended (but possible):
If you want to use a syntax more similar to that in Python (with a list of elements instead of a vector of indices), you need the deal function. But the resulting statement is more complicated, and varies depending on whether arr is a cell array or a standard array. So it's not recommended (for exchanging two elements). I include it only for completeness:
[arr{i}, arr{j}] = deal(arr{j}, arr{i}); %// for a cell array
[arr(i), arr(j)] = deal(arr(j), arr(i)); %// for a numeric or char array
Not to confuse things, but let me another syntax:
[arr{[i,j]}] = arr{[j,i]};
or
[arr{i},arr{j}] = arr{[j,i]};
The idea here is to use comma-separated lists with curly-braces indexing.
Remember that when working with cell-arrays, ()-indexing gives you a sliced cell-array, while {}-indexing extracts elements from the cell-array and the return type is whatever was stored in the specified cell (when the index is non-scalar, MATLAB returns each cell content individually as a comma-separated list).

Weird array assignment in MATLAB: [x,t]=house_dataset

I'm learning work with neural networks through MATLAB samples. In one sample of the documentation (R2012a), there is a weird assignment
[x,t] = house_dataset
Basically, house_dataset is a 13×506 2D array. But the assignment results in two arrays:
x, a 13×506 2D array which is to be used as input to our neural network; t a 1×506 array which is to be used as target for network.
I don't know how this is done. Is it based on some fundamental thing I don't know about MATLAB matrices?
I even assigned house_dataset into another variable
h_dataset = house_dataset;
and then MATLAB gave an error when I tried to do this:
[x,t] = h_dataset;
Error message reads:
>> [x,t] = h_dataset;
Too many output arguments.
Does anyone know what this is all about?
It is normal behaviour for functions (and house_dataset is one of many functions in toolbox)
Function returns 2 values
function [inputs,targets] = house_dataset
but if you just enter
variable = house_dataset;
it returns and saves to variable only first value which is [inputs]
check the behaviour of very simple function
function [out1,out2] = test
out1 = 'first out';
out2 = 'second out';
end
and then call in matlab command window:
[first, second] = test
first = test
second = test
if you want to get only second value use something like:
[~,second] = test

Resources