Elementwise diff() with an condition

12 ビュー (過去 30 日間)
Philipp
Philipp 2013 年 3 月 13 日
I have an array of cells which contains numeric arrays of different lengths, lets call that array X. I want to write a function that writes the diff(cell2mat(X(i))) to and array of cells Y(i). The problem is when I want to add a condition i.e. only write the diff of two elements in the result vector when the difference is <= B. Below my code:
function[Y] = diffleq2(X)
len = length(X);
for i = 1:len
Y(i) = {diff(cell2mat(X(i)))};
end
X is of the form of
X = {[1,2,4,5],[23,12,13]};
just with a lot more cells. Obviously the new function would look like this
function[Y] = diffleq2(X,B)

採用された回答

ChristianW
ChristianW 2013 年 3 月 13 日
X = {[1,2,4,5],[23,12,13]};
Y = cellfun(@diff,X,'un',0);
B = 1.5;
Ycut = cellfun(@(x) x(abs(x)>B),Y,'un',0);
  1 件のコメント
Philipp
Philipp 2013 年 3 月 13 日
編集済み: Philipp 2013 年 3 月 13 日
Thanks for the answer. The first part works nicely (I didn't know about the cellfun function) - this is great.
But I still have problem with the last line where I want to remove all entries > B. Ycut is just the same as Y. Can you explain me the syntax of the function you passed as argument?
Edit: Figured it out - thanks again :)

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 3 月 13 日
編集済み: Azzi Abdelmalek 2013 年 3 月 13 日
X = {[1,2,4,5],[23,12,13]}
B=1
for k=1:numel(X)
b=diff(X{k})
b(b>B)=[]
out{k}=b
end

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by