How do I subtract number from Cell?

1 visualización (últimos 30 días)
Jacob Assayag
Jacob Assayag el 29 de Mayo de 2021
Editada: Jan el 7 de Jun. de 2021
I have a cell That is 6x2 and im trying to subtract 1 (or add -1 ) to the first row and it tell me that -/+ does not support type "cell" .. i tried to convert to double but it also did not let me .. i added a photo of what i tried-
^Trying to subtract 1 from here
thank you !!
  4 comentarios
Jan
Jan el 30 de Mayo de 2021
Editada: Jan el 30 de Mayo de 2021
A simplification for the code:
for i = 1:length(Dice1)
for j = 1:length(Dice2)
d = abs(i - j) + 1;
Source(d) = Source(d) + Dice1(i) * Dice2(j);
end
end
More efficient, by harder to understand:
ind = abs((1:length(Dice1)) - (1:length(Dice2)).') + 1;
val = (Dice1 .* Dice2.');
Source = accumarray(ind(:), val(:)).';
Jacob Assayag
Jacob Assayag el 30 de Mayo de 2021
thanks but any addressing to my question?

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 30 de Mayo de 2021
Editada: Jan el 7 de Jun. de 2021
Dice1=[1/7 1/7 1/7 1/7 1/7 2/7];
Dice2=[1/7 1/7 2/7 1/7 1/7 1/7];
ind = abs((1:length(Dice1)) - (1:length(Dice2)).') + 1;
val = (Dice1 .* Dice2.');
Source = accumarray(ind(:), val(:)).';
HuffTree = huffmandict(1:6,Source);
X = cell2mat(HuffTree(:,1)) - 1;
HuffTree(:,1) = num2cell(X);
More direct:
HuffTree(:,1) = cellfun(@(x) {x - 1}, HuffTree(:, 1))
HuffTree = 6×2 cell array
{[0]} {[ 0 0 1]} {[1]} {[ 0 1]} {[2]} {[ 1 0]} {[3]} {[ 1 1]} {[4]} {[0 0 0 0]} {[5]} {[0 0 0 1]}
But why not creating the Tree correctly from the beginning?
HuffTree = huffmandict(0:5, Source);

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by