MATLAB Answers

Access data from a cell array

1 ビュー (過去 30 日間)
Rajvi Amle
Rajvi Amle 2021 年 7 月 14 日
コメント済み: Rajvi Amle 2021 年 7 月 17 日
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 件のコメント
Devanuj Deka
Devanuj Deka 2021 年 7 月 15 日
Thanks for clarifying @Rajvi Amle. I have posted an answer with a solution, please check if this is what you wanted.

サインインしてコメントする。

採用された回答

Devanuj Deka
Devanuj Deka 2021 年 7 月 15 日
編集済み: Devanuj Deka 2021 年 7 月 15 日
@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 件のコメント
Rajvi Amle
Rajvi Amle 2021 年 7 月 17 日
@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.

サインインしてコメントする。

その他の回答 (1 件)

ANKUR KUMAR
ANKUR KUMAR 2021 年 7 月 14 日
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 件のコメント
ANKUR KUMAR
ANKUR KUMAR 2021 年 7 月 14 日
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?

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by