"???In an assignment A(I) = B, the number of elements in B and I must be the same."

I have two matrix A & B. Matrix A size 1x1013 double and matrix B size 1x12931 double. I want to convert each element of matrix A into base-N from matrix B. For example, first element of matrix A converted into first element of matrix B, second element of matrix A converted into second element of matrix B, and so on.
mm = length(A);
nn = length(B);
base = cell(1,mm);
for ff = 1:mm;
x(ff) = dec2base(A(ff),B(ff));
base{ff} = x;
end
I got error:
???In an assignment A(I) = B, the number of elements in B and I must be the same."
How to fix it? Thank you.

Answers (1)

Use these lines:
for ff = 1:mm
x(ff) = str2double(dec2base(A(ff),B(ff)));
base{ff} = x(ff);
end
the issue was dec2base was returning a character string, not a number.

5 Comments

Still got a problem.
First argument must be an array of integers, 0 <= D <= 2^52.
B(ff) must be at least 2. Here is my complete test script, which runs fine:
A = randi(9,1,1013);
B = randi(9,1,12931)+1;
mm = length(A);
nn = length(B);
base = cell(1,mm);
x = zeros(1,mm)
for ff = 1:mm
clc;
ff
A(ff)
B(ff)
str=dec2base(int32(A(ff)), int32(B(ff)))
x(ff) = str2double(dec2base(A(ff),B(ff)));
base{ff} = x(ff);
end
Yes, the min and max value on matrix A are 3 - 1.0141e+31 and on matrix B are 2 - 36. I already try your solution but there's still error.
??? Error using ===> dec2base at 24
First argument must be an array of integers, 0 <= D <= 2^52.
Error in ==> TestProgram at 32
x(ff) = str2double(dec2base(A(ff),B(ff)));
Cast your A to int32 before you run the loop.
I already tried your solution, but still got the same warning. Why?
mm = length(A);
nn = length(B);
base = cell(1,mm);
x = zeros(1,mm)
for ff = 1:mm
ff
AA=A(ff)
BB=B(ff)
x(ff) = str2double(dec2base(int32(AA),int32(BB)));
base{ff} = x(ff);
end

Sign in to comment.

Categories

Asked:

on 9 Feb 2013

Community Treasure Hunt

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

Start Hunting!