Access data from a cell array

23 views (last 30 days)
Rajvi Amle
Rajvi Amle on 14 Jul 2021
Commented: Rajvi Amle on 17 Jul 2021
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 Comments
Rajvi Amle
Rajvi Amle on 14 Jul 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 on 15 Jul 2021
Thanks for clarifying @Rajvi Amle. I have posted an answer with a solution, please check if this is what you wanted.

Sign in to comment.

Accepted Answer

Devanuj Deka
Devanuj Deka on 15 Jul 2021
Edited: Devanuj Deka on 15 Jul 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 Comments
Rajvi Amle
Rajvi Amle on 16 Jul 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 on 17 Jul 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.

Sign in to comment.

More Answers (1)

ANKUR KUMAR
ANKUR KUMAR on 14 Jul 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 Comments
Rajvi Amle
Rajvi Amle on 14 Jul 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 on 14 Jul 2021
Edited: ANKUR KUMAR on 14 Jul 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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!