How do I subtract number from Cell?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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(:)).';
Réponse acceptée
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))
But why not creating the Tree correctly from the beginning?
HuffTree = huffmandict(0:5, Source);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!