How can I loop all elements in a matrix from a cell array

1 回表示 (過去 30 日間)
BoIs
BoIs 2016 年 11 月 3 日
編集済み: BoIs 2016 年 11 月 5 日
I have a cell array where the cells are something like this: [ 92x1 double] [215x1 double] [127x1 double] [120x1 double] [ 89x1 double] ....and so on(1000 odd cells). I want to be able to extract the first n and last n elements in each of those arrays and get their means. I want all of that in one loop. I tried using a 'for' loop for this but it only gets the required elements from either the first or the last cell array and not all the cells. Im quite new to this and any help would be great! Thanks
if true
% code
end
for i=1:nfiles,
a{i} = diff(w{i}(:,2))./diff(w{i}(:,1));
for k=1:numel(a{i}),
begin_el= a{i}(1:5),
e_elements = a{i}(end-4:end),
end
end

採用された回答

dbmn
dbmn 2016 年 11 月 3 日
One suggestion is to use the running variable k in the loop
begin_el(end, :)= a{k}(1:5),
e_elements(end, :) = a{k}(end-4:end)
another suggestion would be to use a cellfun instead of the for loop.
  1 件のコメント
BoIs
BoIs 2016 年 11 月 4 日
編集済み: BoIs 2016 年 11 月 5 日
Thanks for the suggestion!
I tried cellfun and it worked perfectly

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by