Manipulating arrays within a cell array

8 ビュー (過去 30 日間)
mathman
mathman 2018 年 1 月 4 日
編集済み: mathman 2018 年 1 月 7 日
I have a cell array, A_C which has 401 cells (1x401) each with arrays of varying sizes, but same dimensionality. I'm trying to divide each element within an array by the number of elements within that array. And then replace the array with the new one.
So for example, the array A(1,2) = 1x101, I want to divide each element within this array by 101, B = A(1,2)/101 and then I want to replace the array at A(1,2) by B.
  1 件のコメント
mathman
mathman 2018 年 1 月 4 日
I think I solved it, I'll leave the thread up incase someone wants to do something similar.

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

採用された回答

Jan
Jan 2018 年 1 月 4 日
編集済み: Jan 2018 年 1 月 4 日
% Create some test data: (your code)
dxs = 0:5e-06:0.002;
nCell = length(dxs);
xs = cell(1, nCell); % Pre-allocate!!!
for k = 1:nCell
xs{k} = 0:dxs(k):5e-04;
end
% Divide each cell by its length:
B = cell(size(xs)); % Pre-allocate!!!
for k = 1:nCell
B{k} = xs{k} / length(xs{k});
end
Simple with a loop, isn't it? I find "nCell" nicer than "num_cells_dxs" and avoid "l" as variable, because it is confused with 1 easily.
I assume the cellfun is nicer, but slower:
B = cellfun(@(c) c / length(c), xs, 'UniformOutput', false)

その他の回答 (1 件)

mathman
mathman 2018 年 1 月 4 日
for n = 1:l
xs_current1{n} = xs_current1{1,n}/num_point_sources(1,n);
end

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by