I have a cell (called AA ) that contains 1 row * 36 columns (as shown in the attached image)
As we could see that each column in that cell is a matrix ( inside each column there are 1*3 data points )
I need to have an array that has 36 Rows * 3 columns in MATLAB
For example,
the first column in the Cell will be converted into 1 Row and three columns, the second column in the Cell will be converted in the same way and finally put them all of them together in order to generate an array that contains 36 Rows and 3 Columns
as shown in this example
-1.48247427405830e-15 0.185513882360673 -0.185513882360676
-9.59200039657764e-16 0.211729497802758 -0.211729497802760
3.69087930153418e-16 0.224791092084074 -0.224791092084073
You can just use cat to concatenate the rows and use {:} indexing to create a comma separated list
output = cat(1, AA{:})
In addition to the answer of the gentleman Suever these are 2 methods as well that can be used to solve the same question
output =cell2mat(AA(:))
or, more simply,
output = vertcat(AA{:})
Related
I have a cell with dimension 41X41 as shown below
that has values equal to 1, it means all its values are equal to ones as shown below:
Based on many values, I could not include it here in one figure, but its dimension is 41 x 41.
What I was trying to do is calculate the number of ones in each row minus one as shown in the snippet of line code below:
ccc = sum(isSmaller{cc,:} == 1)-1
In order to get a cell with 1 row with 40 columns dimension that has 40s. as shown below:
My problem is instead of 40 columns is still showing 41 columns after deleting one from each row as indicated below.
May I get assistance, please? I need it to be 1 row with 40 columns.
Note: I do not care for the first 40 rows or columns or the lasts or the middle ones. I just need it to be done by itself only by minus one.
After that, I need to sum all the 40s in this ccc = 1x40.
Below is my try:
for cc = 1:length(isSmaller)
ccc = sum(isSmaller{cc,:} == 1)-1
end
If I understand correctly, isSmaller is a 1-by-1 cell array whose single cell contains a 41-by-41 matrix of logical values all equal to 1 (true):
isSmaller={true(41)};
Then you wish to calculate the sum of each row of this matrix inside of a cell and subtract one from the result:
sum(isSmaller{1},1)-1
But since you only care about the first 40 rows you can trim off the last row with:
sum(isSmaller{1}(:,1:40),1)-1
Or:
sum(isSmaller{1}(:,1:end-1),1)-1
This returns a 1-by-40 matrix where each element is 40.
Finally, you want to sum over the 40 elements of this matrix:
sum(sum(isSmaller{1}(:,1:end-1),1)-1)
This returns 1600, as expected.
No for loops are needed if this is all you need to do and your example is representative. I urge you to read through Matlab's documentation on cell arrays, in particular on how to access data within them.
I have a cell data containing one row and four column. What i am trying to do is to go through elements in first column and see if an year '2018' is seen in the first column of each cell. When i perform this code, it will only give me the indices containing '2018' in the fourth cell. Not all the cells. Any help will be appreciated.
time = cellfun(#x, contains(x,'2018'),data{ii}(:,1))
I have created a small data sample as part of an example which I hope will help you solve the problem.
%Create cell array of strings.
data = {["2018","date","2018";"stack","cell2018","fun"],["data","stackoverflow","2018";"array","variable","data2018"]}
%Search for substring 2018 in the first column of every cell.
time = cellfun(#(x) contains(x(:,1),'2018'),data,'UniformOutput',0)
The output of time is a logical cell array:
>>time
{2×1 logical} {2×1 logical}
>>time{1}
1
0
>>time{2}
0
0
For the first cell, the column contains 2018 and stack, therefore 1 and 0 are returned.
For the second cell, the column contains data and array, therefore 0 and 0 are returned.
If you wish to find the indexes from the logical array, you can use the find function with outputs [row,col].
I have a matrix that contains numerical data. Size being 31x48. I am wanting to add a label to each row of this matrix. The best way i can think of is to convert my matrix to a cell array with each row containing my 48 pieces of data.
I am unsure about how to program this so it iterates through each row adding a label such as 'Day 1' and then containing my data.
I want it to look like the following:
Day 1 [30 30 30 30 30 .......]
Day 2 [30 30 30 30 30 ...] etc.
I need the label to be assigned to the row so that when I split it into two groups I know what data corresponds to what day.
We'll start from your original <31x48> matrix. We'll call it 'mat'
mat=rand([31,48])
At first, we convert the matrix to cell array
mat_cell=num2cell(mat,2)
This will create a <31x1 cell>. mat_cell{day,1} will return you the 48 items for that day number. Now we'll add labels to the second column of mat_cell (1st column contains your <1x48> data). Suppose we have a labels name cell array of size <1x31> such that
labels={'Day 1' ; 'Day 2' ; 'Day 3' ; ... 'Day 31'}
then
mat_cell(:,2)=labels
should add labels to each row of mat_cell. Then
mat_cell{day,1} returns the <1x48> array for the 'day'.
mat_cell{day,2} returns label for the 'day'.
Please Note: Although it is quite evident, the labels cell array should have text arranged in correspondence to the columns in your original mat array.
It is a basic problem but I am not so much experienced in Matlab(Guide).
What I have now is a cell array called Z with 21x2 elements: 21 rows 2 columns.
What I would like to do is to get only the first column (to show only 21x1).
Then, in this column there is a list of names. Inside the 21 rows of this cell there are repeated names. I would like to run through each row of this 21x1 column, detect which are repeated. The repeated ones should be printed in the uitable in a white colour.
Any ideas?
I believe this should deal with the core of your question:
A={'abc' 6;'de' 7;'abc' 8};
[C, ia] = unique(A(:,1));
idx = setdiff(1:size(A,1),ia);
A(idx,1)
This code will list all duplicates.
Hi I have a cell array 2 x 1000. the first column holds numeric (double) values, the second holds a string. i would like the find all cells in the first column that are above a certain value, and bring back the corresponding cells in the second column. I have tried strcamp and various others but obviously they are for strings. I also tried doing
sortrows(mycell(1,:));
so i could pick the first 50 rows off or whateever, but this didn't seem to order the cell array. but really i would like to specifiy a threshold on the first column of the cell array.
How do I do this?
thanks.
If C is your cell array:
nums = [C{:,1}];
{:} converts C into a comma separated list (so {:,1} only converts the first column) and then [] collects the results into a normal array. After that it's simple:
index = nums > Threshold;
C(index,:)
OR in a one liner:
C([C{:,1}] > Threshold, :) %// Or C([C{:,1}] > Threshold, 2) as Luis said