subtraction on the cells elements

3 visualizaciones (últimos 30 días)
johnson saldanha
johnson saldanha el 11 de Dic. de 2018
Comentada: Guillaume el 11 de Dic. de 2018
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 comentario
johnson saldanha
johnson saldanha el 11 de Dic. de 2018
Editada: Guillaume el 11 de Dic. de 2018
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.

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 11 de Dic. de 2018
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 comentarios
johnson saldanha
johnson saldanha el 11 de Dic. de 2018
ifi have to make the first row in every cell element as 0 how do i do it
Guillaume
Guillaume el 11 de Dic. de 2018
Replace vec(1) or x{idx}(1) by 0.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by