How to create column cells, where each cell contains matrix - matlab

I have a function in Matlab that requires as the input
column cells, where each cell contains an SPD matrix
To be more precise this function requires 3 input arguments, the first two are column cells, where each cell contains an SPD matrix, But I don't know how to define a column cell in Matlab. I have tried this:
TestData(:,:,12) = T;
TestData is supposed to be my cell column and T is a matrix that should be in this column. for every matrix that I have, I put it in a variable called T and then using above command I add it to a 3D array. So the first matrix is in TestData(:,:,1), The second one is in TestData(:,:,1) and so on. When I run my function with TestData as the input variable i get this error:
Cell contents reference from a non-cell array object.
So I think I didn't define a cell column right.

A 3D array is not a cell. If you want each 3D slice of your 3D array to be a separate cell element, you could use num2cell followed by a call to squeeze to remove all singleton dimensions and make it an N x 1 cell array.
inputs = squeeze(num2cell(TestData, [1 2]));

Related

Matlab combine cells and strings into a cell [duplicate]

I have created a function which takes vectors for input variables and returns a cell array for each set of inputs. The final output variable (out) seems to consist of a 2x1 cell containing two 1x5 cells. I have provided a screenshot of this below:
I am just trying to figure out how to flatten the cell array (out) to be a 2x5 cell array.
One way to achieve that would be -
vertcat(cell_array1{:})
If your cell has unequal number of elements in each row , maybe this might work better
vector=[cell_array{:}]

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)

Indexing data in matlab

I have imported a lot of data from an excel spreadsheet so that I have a 1x27 matrix.
I have imported data from excel using this
filename = 'for_matlab.xlsx';
sheet = 27;
xlRange = 'A1:G6';
all_data = {};
for i=1:sheet,
all_data{i} = xlsread(filename, i, xlRange);
end
However each element of this all_data matrix (which is 1x27) contains my data but I'm having trouble accessing individual elements.
i.e.
all_data{1}
Will give me the entire matrix but I need to perform multiplications on individual elements of this data
also
all_data(1)
just gives '5x6 double', i.e. the matrix dimensions.
Does anybody know how I can divide all elements of each row by the third element in each row and do this for all of my 'sub-matrices' (for want of a better word)
Assuming that all_data is a cell array and that each cell contains a matrix (with at least three columns):
result = cellfun(#(x) bsxfun(#rdivide, x, x(:,3)), all_data, 'uniformoutput', 0);
You are mixing terminology in matlab. what you have is 1x27 CELLS each of them containing a matrix.
If you access all_data{1} it will give you the whole matrix stored in the first cell.
If you want to access the elemets of that matrix then you need to do: all_data{1}(2,4). This example access the 2,4 element of the matrix in the first cell.
Definitely Luis Mendo has solved you problem, but be aware of the differences of Cells and matrixes in Matlab!
Okay I have found the answer now.
Basically you have to use both types of brackets because the data types are different
i.e. all_data{1}(1:4) or something like that anyway.
Cheers

strcat generates 1x1 cell in matlab

Consider the following snippet:
f=strcat(s,emotions{emotion},int2str(i),'\mean.points');
f1=strcat(s1,speakers(speaker),emotions{emotion},int2str(i),'\mean.points');
Here emotions and speakers are 1x7 and 1x4 arrays. The rest are strings and integers.
The type of f1 comes out to be 1x1 cell while f remains a string. What could be the difference between the two?
Since it comes out to be a 1x1 cell I can't use it for fopen() without using an index.
If any input is a cell array, combinedStr is a cell array of strings. Otherwise, combinedStr is a character array.
In f you concatenate just char-arrays, but in f1 obviously appears a cell array speakers(speaker).
So just use speakers{speaker} also, and it should work.
With () you are indexing the cell array, therefore you get a cell element. With {} you are addressing the content of the specified cell.

creating a new matrix from a cell array after evaluating one column.

I have a cell array of 447*1 Dimensions. The cell array has 2Dimensional arrays of different dimensions of type double. I want to check a particular value in that cell array compare and on that basis store it in a new Matrix.
So for example my my starting cell array is Y{447*1} . My first cell contains an array of
5*10 and second array contains data of 22*10 . I want to evaluate the second column
of this array and then store it in a new Matrix.
I did this for one set of data and the code looks something like this.
A = [y{2,1}(1:20,2),y{4,1}(1:20,2),y{6,1}(1:20,2),y{8,1}(1:20,2),...
y{10,1}(1:20,2),y{12,1}(1:20,2),y{14,1}(1:20,2),y{16,1}(1:20,2),...
y{18,1}(1:20,2),y{20,1}(1:20,2),y{22,1}(1:20,2),y{24,1}(1:20,2),...
y{26,1}(1:20,2),y{28,1}(1:20,2),y{30,1}(1:20,2)];
But I want to automate the thing. Please help how this can be done.
Something along the lines of:
Temp = cellfun(#(x) x(1:20,2),Y(1:2:end,1), 'UniformOutput', false);
A = cat(2,Temp{:});
Should work if I am reading your question right - it should replicate your example anyway.
You can then change the dimensions of the #(x) function x(1:20,2) to take out different values from your cell array, and use different cell indexing for Y(:,1) to pick different parts of Y.