How do I subtract number from Cell?
1 view (last 30 days)
Show older comments
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 Comments
Jan
on 30 May 2021
Edited: Jan
on 30 May 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(:)).';
Accepted Answer
Jan
on 30 May 2021
Edited: Jan
on 7 Jun 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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!