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

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.

Related

How to create column cells, where each cell contains matrix

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]));

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)

MATLAB: If this value of 5x5 cell with vectors [106x1] are different to zeroes,count them e put the count in a matrix

I have matchcounts (5x5)cell, every cell has a vector of double [106x1]. The vectors of double have zeros and non zero values. I want to find non zero values for every cell, count them and put the result in a matrix.
I tried with this code:
a{i,j}(k,1)=[];
for k=1:106
for i=1:5
for j=1:5
if (matchcounts{i,j}(k,1))~=0
a{i,j}=a{i,j}(k,1)+1;
end
end
end
end
and others but it's not correct! Can you help me? Thanks
While it is possible to fix your answer above, I recommend to change the data structure to have a much simpler solution possible. Instead of having a 2D cell array which holds 1D data, choose a single 3D data structure.
For an optimal solution you would change your previous code code to directly write the 3D-matrix, instead of converting it. To get started, this code converts it so you can already see how the data structure should look like:
%convert to matrix
for idx=1:numel(matchcounts)
matchcounts{idx}=permute(matchcounts{idx},[3,2,1]);
end
matchcounts=cell2mat(matchcounts);
And finding the nonzero elements:
a=(matchcounts~=0)
To index the result, instead of a{k,l}(m,1) you use a(k,l,m)
To give you some rule to avoid complicated data structures in the future. Use cell arrays only for string data and data of different size. Whenever you have a cell array which contains only vectors or matrices of the same size, it should be a multidimensional matrix.

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