Access data from a cell array

I have a cell array of 1x10 where each of the 10 arrays are matrices of 138x18. If I want to access the data of each of the cells but just want to calculate the sum of (138,1) and (138,2) from each arrays then how do I calculate and how can I access it?
I tried below to get access through the cell as shown below:
x- 1x10 cell- 138x18 double
X=x{1,10};
m_Au_A1=X(:,1);
m_Au_V1=X(:,2);
I want to calculate the sum of (138,1) and (138,2) from each arrays of cell. How that can be done? Any help would be really appreciated. Thank you in advance.

3 comentarios

I am a bit confused. You mentioned that 'I want to calculate the sum of (138,1) and (138,2) from each arrays of cell'. But in the code you tried:
X=x{1,10};
m_Au_A1=X(:,1);
m_Au_V1=X(:,2);
Here in 'm_Au_A1' you extracted the entire first column of the array in the 10th cell of the 1x10 cell array.
Do you want the sum of the first two elements of the 138th row in each array? Or do you want the a column vector containing the element wise sum of the entire first and second column in each array?
Rajvi Amle
Rajvi Amle el 14 de Jul. de 2021
@Devanuj Deka Sorry for my incovenience and making a confusion in a question. Yes, I want the column vector containing the element wise sum of the entire first and and second colomn in each array.
Devanuj Deka
Devanuj Deka el 15 de Jul. de 2021
Thanks for clarifying @Rajvi Amle. I have posted an answer with a solution, please check if this is what you wanted.

Iniciar sesión para comentar.

 Respuesta aceptada

Devanuj Deka
Devanuj Deka el 15 de Jul. de 2021
Editada: Devanuj Deka el 15 de Jul. de 2021
@Rajvi Amle, you can try this:
load x % Load the data
Sum = cell(1,10); % Initialize a 1x10 cell array which will store the summed columns from each matrix in 'x'.
for i = 1:10 % For the i'th cell in 'x'
mat = x{i}; % extract the matrix
Sum{i} = mat(:,1) + mat(:,2); % store the sum of the first two columns
end
Here 'Sum' will be a 1x10 cell array, where the i'th cell will store a column vector which is the sum of the first two columns of the matrix in the i'th cell of 'x'. I hope this helps.

9 comentarios

Rajvi Amle
Rajvi Amle el 15 de Jul. de 2021
@Devanuj Deka Thank you for your help. However, I just had a question that the Sum = cell(1,10); contains the sum of all the columns from 1 to 18 from each matrix of x?
No. When initialised, Sum = cell(1,10); is an empty 1x10 cell array.
After the for loop, there is a column vector in each cell of Sum, that is a result of adding the first two columns of the corresponding array in 'x'. For example, If you open Sum{5}, it will be equal to the sum of the first two columns in the array x{5}.
Suppose in the 5th cell of x, you have the array: (we only need the sum column 1 and column 2, so I'm illustrating with a simple 5x5 array. The above code will work for a 138x18 array as well)
x{5} = [1 2 3 4 5;
2 3 4 5 6;
3 4 5 6 7;
4 5 6 7 8;
5 6 7 8 9];
then Sum{5} will contain the column:
3
5
7
9
11
Hope this makes it clear.
Rajvi Amle
Rajvi Amle el 15 de Jul. de 2021
@Devanuj Deka Okay. when I tried to do sum=cell (1,10); it was not an empty cell array but contained 138x1 matrix in each cell. Here I have attached the sum.mat file.
Devanuj Deka
Devanuj Deka el 15 de Jul. de 2021
Editada: Devanuj Deka el 15 de Jul. de 2021
I would advise against using 'sum' as a variable name. It is a keyword in MATLAB, which could cause confusion and buggy code.
Use any other variation of the name, like Sum, sum_col etc.
Rajvi Amle
Rajvi Amle el 15 de Jul. de 2021
@Devanuj Deka Yes changed the name of variable as Sum_col and now I got the answer. Further I am trying to calculate the mean of these i=1:10 iterations all together after the 'for loop'. But how would I again access the Sum_col=1x10 cell array containing each of the 10 arrays with matrices of 138x1 and so on? I have attached the Sum_col.mat file for the reference. Because the dimensions of each cell array containing matrices is not the same. Still is it possible to calculate mean out of Sum_col?
Rajvi Amle
Rajvi Amle el 16 de Jul. de 2021
@Devanuj Deka Do you have any suggestions for the above question to calculate mean? Looking forward to your response.
Devanuj Deka
Devanuj Deka el 16 de Jul. de 2021
Editada: Devanuj Deka el 16 de Jul. de 2021
@Rajvi Amle, If you want to calculate the mean, you'll have to add NaN values to the shorter columns so that all columns in Sum are of equal dimensions. For that, you'll have to modify the section inside the for loop:
for i = 1:10 % For the i'th cell in 'x'
mat = x{i}; % extract the matrix
mat_sum = mat(:,1) + mat(:,2); % store the sum of the first two columns in an array
mat_sum(end+1:139) = missing; % Add NaN values to shorter columns to make them 139x1, which is the dimensions of the longest column in 'x'
Sum{i} = mat_sum; % Store this modified column to the i'th cell of Sum.
end
I used m = missing to fill NaN values. See this MATLAB Answer solution for clarity on how to use it: https://in.mathworks.com/matlabcentral/answers/647178-how-to-add-nan-values-to-a-matrix#answer_543968
Now all cells of Sum have equal dimensions. So using A = cell2mat(C), we can make a 139x10 matrix, on which we can directly apply the M = mean(A,dim) function.
Sum_array = cell2mat(Sum_col);
col_mean = mean(Sum_array,2,'omitnan'); % 2nd argument specifies the dimension along which the sum is to be calculated. Setting it as '2' will calculate the sum along the rows, giving the output as a 139x1 column.
'omitnan' will ensure that ALL the elements are summed - even the last elements in the longer columns.
If you don't pass any 3rd argument in mean, the default parameter will be 'includenan', which will give a NaN value in the output if any column has NaN in that row. For example, the shortest column in 'x' has 134 rows - which means that rows 135-139 in that column are NaN. Without specifying 'omitnan', the output col_mean will have NaN in the last 5 rows, even though other columns might have numeric values in those rows.
Rajvi Amle
Rajvi Amle el 16 de Jul. de 2021
@Devanuj Deka. Yes now I got an answer for mean as I expected. I calculate standard of deviation and confidence interval based on the mean. But my graph (attached as Fig6_CI) shows only a single line instead of showing three lines as upper bound, mean and the lower bound. Following is my code I calculated std and CI so far: Could you please help me with this?.
Sum_col = cell(1,10); % Initialize a 1x10 cell array which will store the summed columns from each matrix in 'x'.
for i = 1:10 % For the i'th cell in 'x'
T=t{i};
T(end+1:139)= missing;
mat = x{i}; % extract the matrix
mat_sum= mat(:,1) + mat(:,2); % store the sum of the first two columns
mat_sum(end+1:139) = missing;
Sum{i} = mat_sum;
end
Sum_array = cell2mat(Sum);
col_mean = mean(Sum_array,2,'omitnan');
% Confidence interval of 95% for simulated data
std=col_mean/size(Sum_array,2); % std dev of mean(y) is mean(y)/nObs
% ts = tinv([0.025; 0.975],length(x1)-1); % T-Score
CI95 = [col_mean col_mean + 1.96.*(std/sqrt(length(mat_sum))) col_mean - 1.96.*(std/sqrt(length(mat_sum)))] ;
Rajvi Amle
Rajvi Amle el 17 de Jul. de 2021
@Devanuj Deka Could you please help me with this if possible? This could be really helpful because i am working on my project and I am totally new in Matlab and trying to learn it.

Iniciar sesión para comentar.

Más respuestas (1)

ANKUR KUMAR
ANKUR KUMAR el 14 de Jul. de 2021
Let us create a random data.
random_data_cell=arrayfun(@(x) rand(138,18), 1:10, 'uni',0);
Let merge these data set in a matrix.
random_data_matrix=cat(3,random_data_cell{:});
size(random_data_matrix)
ans = 1×3
138 18 10
% "I want to calculate the sum of (138,1) and (138,2) from each arrays of cell."
sum_of_138_1=sum(random_data_matrix(138,1,:))
sum_of_138_1 = 5.7619
sum_of_138_2=sum(random_data_matrix(138,2,:))
sum_of_138_2 = 3.4424

2 comentarios

Rajvi Amle
Rajvi Amle el 14 de Jul. de 2021
@ANKUR KUMAR Sorry for my incovenience and mistakenly made a confusion in a question. I want the column vector containing the element wise sum of the entire first and and second colomn in each array.
ANKUR KUMAR
ANKUR KUMAR el 14 de Jul. de 2021
Editada: ANKUR KUMAR el 14 de Jul. de 2021
If you want to calculate the element wise sum, you can do it using the sum commands.
random_data_cell=arrayfun(@(x) rand(138,18), 1:10, 'uni',0);
random_data_matrix=cat(3,random_data_cell{:});
output=sum(random_data_matrix,3);
output
output = 138×18
5.9721 5.4324 6.1655 4.4693 4.7727 5.7600 5.7924 5.5296 3.4552 5.3622 5.2822 6.8909 6.3871 4.5325 6.0799 4.7366 4.5823 5.3439 4.5680 5.1044 3.8329 4.1000 6.6097 3.6770 4.1740 4.1137 5.9776 2.2656 4.5354 5.6484 4.2640 3.4529 5.0674 4.1760 4.9397 5.4225 4.0440 4.0330 2.7096 4.4737 4.7923 4.8489 3.7870 4.8415 4.1043 5.1490 2.5537 5.4141 4.1984 5.1140 5.3451 5.3463 4.5572 5.0309 4.0869 4.5015 5.9514 5.2031 3.4604 5.1385 5.5002 4.3508 5.4030 6.8004 4.6312 4.2704 4.8556 6.2446 4.5646 4.4551 3.9234 5.3623 4.4039 5.4754 5.7783 3.5712 6.6286 4.8854 4.6261 5.3618 6.3978 6.9742 5.0644 5.9297 5.1274 4.4751 5.1649 3.7136 5.3540 3.2868 7.5646 5.1302 4.7648 4.7789 6.0271 6.0025 5.5054 5.1250 4.6288 5.6412 5.0644 5.7957 5.3603 4.2789 5.3654 3.3256 5.2350 5.0227 6.2739 4.5899 5.0476 5.5893 5.4855 4.8651 4.5546 4.8745 4.7439 8.0537 4.8122 6.4072 3.0733 5.1822 3.7056 6.1357 4.8739 5.3994 5.0761 3.7176 5.6164 5.1590 5.4305 5.4357 5.0424 5.4848 5.5599 5.2829 4.6477 3.5005 4.6719 4.4396 5.6583 5.7473 5.0443 6.3915 4.5823 4.8743 4.5528 4.9836 3.7416 6.4809 5.1090 5.7665 4.7820 4.4281 4.3189 3.8533 4.4411 3.8667 5.5423 4.9134 5.7209 5.3886 4.5839 4.3828 5.0645 5.3717 4.6212 5.9840 4.5980 6.1460 5.1197 6.8984 6.1678 4.1784 5.1346 5.4579 3.3217 5.2189 5.0166 5.4038
size(output)
ans = 1×2
138 18
If this is not the expected output, please let us know the dimension of your expected output. What would be the dimension of the output you are expecting?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 14 de Jul. de 2021

Comentada:

el 17 de Jul. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by