subtraction on the cells elements

3 Ansichten (letzte 30 Tage)
johnson saldanha
johnson saldanha am 11 Dez. 2018
Kommentiert: Guillaume am 11 Dez. 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 Kommentar
johnson saldanha
johnson saldanha am 11 Dez. 2018
Bearbeitet: Guillaume am 11 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by