Octave appending in a 2D cell array - matlab

I'm trying to append an element at the end of a 2D cell array row. My code is:
b = cell(5, 0)
b(1) = {b(1, :), 2} % Trying to append at the end of the first row
This gives me the error: error: A(I) = X: X must have the same size as I
I've also tried various other forms, such as:
b = cell(5, 0)
b(1, end+1) = 2 % Ok, inserts 2 at [1,1]
b(2, end+1) = 3 % No, inserts 3 at [2,2] instead of [2, 1]

It seems that you are confused with cell array indexing.
If you want to append elements at the end of a row in a matrix (in your case, a cell array), you must still make sure that all rows are of the same size after the assignment, otherwise you'll trigger an error about mismatching dimensions.
Instead of b(1) = {b(1, :), 2}, the following should work:
b(1, end + 1) = 2
Alternatively, if you want to append an entire column array of cells to b, use horizontal concatenation, for example:
b = [b, {2; 3; 4; 5; 6}];
This should append a single cell at the end of each row of b.

The reason the element gets inserted at [2, 2] and not [1, 1] is that by the time you try to insert the second element, the value denoted by end has increased from 0 to 1.
The following should do what you need:
>> b = cell(5, 0)
b =
Empty cell array: 5-by-0
>> b(1,1) = {2}
b =
[2]
[]
[]
[]
[]
>> b(2,1) = {3}
b =
[2]
[3]
[]
[]
[]
>>

Related

How to combine two matrices column wise to strings separated by comma?

I have two matrices
A = [ 1 3
4 3]
B = [ 2 1
4 1 ]
I want to combine A and B to produce the string array
C = [ "1,2" "3,1"
"4,1" "3,1" ]
How can I do this in MATLAB? I tried it this way
for i = 1: 4;
for j = 1: 4;
fprintf('%0.2f,%0.2f\n',A(i,j),B(i,j) )
end
end
Appreciate your suggestions !
A = [1 3; 4 3];
B = [2 1; 4 1];
C = A + "," + B
C =
% 2×2 string array
% "1,2" "3,1"
% "4,4" "3,1"
The first thing to note, is that there is a difference between strings "string" and character arrays 'character array'. Whereas strings are one entity, the character array is an array of characters.
Thus you can make the following assignment
A(1) = "Hello";
but not
B(1) = 'Hello';
because the B(1) is one value, and 'Hello' is 5 values (H,e,l,l,o).
Secondly, you cannot use fprintf as you suggest in the comments as it only prints (as in its name) and the variable returned by fprintf is the number of characters printed. Instead, to construct the string use strcat together with num2str, such that you get:
A = rand(2); %some matrices
B = rand(2);
for i = 1:2
for j = 1:2
C(i,j) = strcat(num2str(A(i,j)),",",num2str(B(i,j)));
end
end
EDIT: If you are anyway going to interchange the comma for \pm in LaTeX, you can just do it when constructing C by using
C(i,j) = strcat(num2str(A(i,j)),"\pm",num2str(B(i,j)))
instead.

How to get all unique values in a cell array of matrices?

I want to get all unique values in A, where A is a cell array of matrices of different shapes and sizes:
A = {[], 1, [2 3], [4 5; 6 7]};
U = [];
for ii = 1: numel(A)
a = A{ii};
U = [U; a(:)];
end
U = unique(U);
That returns:
U =
1 2 3 4 5 6 7
If all elements in A where row vectors, I could use [A{:}] like:
U = unique([A{1:3}]);
That returns:
U =
1 2 3
But in my case it throws an exception:
Error using horzcat
Dimensions of matrices being concatenated are not
consistent.
So how can I avoid that for-loop?
You can use cellfun to reshape all elements in the cell.
U = unique(cell2mat(cellfun(#(x)reshape(x,1,numel(x)),A, 'UniformOutput', false)));
or avoiding the reshapewith
U = unique(cell2mat(cellfun(#(x)x(:).',A, 'UniformOutput', false)));
We can go this way:
A = {[], 1, [2 3], [2 0; 4 5; 6 7]};
AA = cellfun( #(x) unique(x(:)), A, 'UniformOutput' , false)
res = unique(cat(1, AA{:}))
First of all create unique array for each cell - it let us avoid converting all the cells to numeric just only unique values.
Lets convert cell array to one numeric array - cat(1, AA{:}) .
Find unique values through this resulting array.

Finding elements in an array other than given indices

I want to find the elements in an array which are not the given index elements. For example, given an array A = [1 5 7 8], and index ind = [2 3], the operation should return elements [1 8].
Use a direct index vector:
B = A(setdiff(1:numel(A),ind));
Or throw away unneeded elements:
B = A;
B(ind) = [];
Or use logical indexing:
% either
B = A(~any(bsxfun(#eq,ind(:),1:numel(A)),1));
% or
B = A(all(bsxfun(#ne,ind(:),1:numel(A)),1));

Push array onto 2D array (matrix) in MATLAB

For God only knows what reason, we're being asked to use MATLAB in an AI course. All I want to do is initialize an array, and push arrays onto it. In Ruby, this would be:
multi_arr = []
an_arr = [1, 2, 3, 4]
multi_arr << an_arr
Done! Unfortunately I can't find a similarly simple solution in MATLAB.
Any advice would be extremely appreciated.
EDIT: for the interested, here's the rather ungraceful solution I arrived at:
child_states = []
child_state = [0,1,2,3,4,5,6,7,8]
% returns [rows, columns]
dimensions = size(child_states)
child_states(dimensions(1)+1, 1:9) = child_state
You can append array to an array in matlab without knowing the dimensions but it won't be very efficient because matlab will allocate space for the whole array each time you do it. Here's how to do it:
arrays = [];
arr1 = [1,2];
arr2 = [3,4,5];
% append first array
arrays = [arrays ,arr1 ]
% append second array
arrays = [arrays ,arr2 ]
arrays =
1 2
arrays =
1 2 3 4 5
if each of the arrays you want to append have the same length, then you can append them as rows:
arrays = [];
arr1 = [1,2,4];
arr2 = [5,6,7];
% append first array
arrays = [arrays ; arr1 ]
% append second array
arrays = [arrays ; arr2 ]
arrays =
1 2 4
arrays =
1 2 4
5 6 7
for more of a ruby like array appending you should use cell arrays:
cells = {};
cells = [cells ,[4,5] ]
cells = [cells ,[1,1,1] ]
cells = [cells ,['hello']]
cells =
[1x2 double] [1x3 double] 'hello'
GIYF. It seems that you are looking for horzcat and vertcat. Check out MATLAB's doc at Creating and concatenating matrices.; from vertcat page:
C = vertcat(A1,...,AN) vertically concatenates arrays A1,...,AN. All arrays in the argument list must have the same number of columns.
If the inputs are multidimensional arrays, vertcat concatenates N-dimensional arrays along the first dimension. The remaining dimensions must match.
Here's a function that's supposed to do what you want: concatenate a row vector to an array regardless of size. This function will check the dimension along the second axis of input and output array and pad zero to whichever one that is smaller so they can be concatenated along the first axis.
function m = freevertcat(m, n)
if isempty(m)
m = cat(1, m, n);
else
size_m = size(m, 2);
size_n = size(n, 2);
if size_m > size_n
n(size_n+1 : size_n + size_m - size_n) = 0
elseif size_n > size_m
m(:, size_m+1 : size_m + size_n - size_m) = 0;
end
m = cat(1, m, n);
end
example usage
m = []
n = [1,2,3,4,5]
m = freevertcat(m,n)
p = [3,3,3]
m = freevertcat(m,p)
You'll get
m = 1 2 3 4 5
3 3 3 0 0

Array from vector on the basis of a second array

I have a vector v. I need to form an array a containing elements specified according to another array b. Each row in a (let's denote it by r) should contain all elements from v, with starting and ending indices corresponding to the first and last elements given in the matching column in b. For instance:
A(1, :) = v(b(1, 1):b(2, 1));
A(2, :) = v(b(1, 2):b(2, 2));
A(3, :) = v(b(1, 3):b(2, 3));
and so on. Obviously b(2,:) = b(1,:) + constant.
Can I do this without a loop in MATLAB?
Try this:
N=8; P=3; M=5;
v = rand(N,1);
b = zeros(2,M);
b(1,:) = [1 2 4 5 6];
b(2,:) = b(1,:) + P - 1;
A = cell2mat(arrayfun(#(i0,i1) v(i0:i1),b(1,:),b(2,:),'UniformOutput',false))'
You can use linear indexing and bsxfun to directly access the elements:
A = v(bsxfun(#plus, b(1,:).', 0:b(2,1)-b(1,1)));