How do I subtract number from Cell?

2 vues (au cours des 30 derniers jours)
Jacob Assayag
Jacob Assayag le 29 Mai 2021
Modifié(e) : Jan le 7 Juin 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 commentaires
Jan
Jan le 30 Mai 2021
Modifié(e) : Jan le 30 Mai 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 le 30 Mai 2021
thanks but any addressing to my question?

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 30 Mai 2021
Modifié(e) : Jan le 7 Juin 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);

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by