How do I subtract number from Cell?

1 view (last 30 days)
Jacob Assayag
Jacob Assayag on 29 May 2021
Edited: Jan on 7 Jun 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 Comments
Jan
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(:)).';
Jacob Assayag
Jacob Assayag on 30 May 2021
thanks but any addressing to my question?

Sign in to comment.

Accepted Answer

Jan
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))
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);

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!