Access data from a cell array
13 ビュー (過去 30 日間)
古いコメントを表示
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
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
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 件のコメント
その他の回答 (1 件)
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)
% "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_2=sum(random_data_matrix(138,2,:))
2 件のコメント
ANKUR KUMAR
2021 年 7 月 14 日
編集済み: 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
size(output)
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?
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!