subtraction on the cells elements

3 次查看(过去 30 天)
i have a cell array x of size 12*1
and the cells are column vectors of type double
x{1,1}=[1 2 5 6 7 7 8 ];
x{2,1}=[ 1 2 6 6 7 8 8]
and so on
i want the output y as a cell array with
y{1,1}=[1 1 3 1 1 0 1];
y{2,1}=[1 1 4 0 1 1 0];
the elements of the cell in the output should be the difference i-(i-1). let the first element be as it is it. start with i =2:length x{i,1}
for each each cell the operation shouldbe done and stored in a different cell
  1 个评论
johnson saldanha
johnson saldanha 2018-12-11
编辑:Guillaume 2018-12-11
acc={}
for i=1:size(out,1)
y=out{i,1};
for i=2:length(y)
acc(i)=y(i)-y(i-1);
end
acc{i,1}=num2cell(acc);
end
Conversion to cell from double is not possible.
Error in accelerationrate (line 66)
acc(i)=y(i)-y(i-1);
i tried this and im getting this error.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2018-12-11
Note that [1 2 5 6 7 7 8 ] in your example is a row vector, not a column vector as you state. If the content of the cells are indeed column vectors then you'll have to change the , into ; where indicated
%demo data
x = num2cell(randi(10, 12, 10), 2)
%processing
y = cellfun(@(vec) [vec(1), diff(vec)], x, 'UniformOutput', false) %replace [vec(1), by [vec(1); if working on column vectors
or if you want to use a loop instead of cellfun:
y = cell(size(x)); %preallocation
for idx = 1:numel(x)
y{idx} = [x{idx}(1), diff(x{dix})]; %replace , by ; for column vectors
end
  2 个评论
johnson saldanha
johnson saldanha 2018-12-11
ifi have to make the first row in every cell element as 0 how do i do it
Guillaume
Guillaume 2018-12-11
Replace vec(1) or x{idx}(1) by 0.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by