How to solve Conversion to cell from double is not possible.

I am trying to create an array named actOld using this code
for i=1:nPlayers
uAve{i}=zeros(nRoads,1);
actOld(i)=randi(3);
end
and the error message " Conversion to cell from double is not possible. , actOld(i)=randi(3);" appears.

8 Comments

Unless you have created a cell array called randi I don't really see how you could get that error on that line.
I didn't.
this is the whole code:
inertia=0.5;
nPlayers=10;
nRounds=10;
nRoads=3;
mycongestion=zeros(nRoads,1);
congustion=zeros(nRoads,1);
congestionPlus1=zeros(nRoads,1);
for i=1:nPlayers
uAve{i}=zeros(nRoads,1);
actOld(i)=randi(3);
end
for i=1:1:nRounds
for j=1:nPlayers
ran=rand(1);
if(ran<inertia || i==1)
act(j)=actOld(j);
else
[~,act(j)]=min(uAve{j});
actOld(j)=act(j);
end % end ran< inertia , else
disp(ran);
disp(act(j));
end % end nPlayers.
for k=1:nRoads
[~,nUsers(k)]= length(find(act==k))
end %end nRoads.
congustion= getCongestion(nUsers);
fprintf(' A= %.0f min B= %.0f min C= %.0f min',congustion(1),congustion(2),congustion(3));
congestionPlus1= getCongestion(nUsers + 1);
for j=1:nPlayers
for k=1:1:nRoads
if (act(j)==k)
myCongestion(k)=congustion(k);
else
myCongestion(k)=congestionPlus1(k);
end %ends act(j)==k , else.
end %end nRoads.
uAve{j}=uAve{j}+(1/j+1)*(myCongestion' - uAve{j});
end % ends nPlayers.
end % end nRounds
actOld{i}=randi(3);
If you actually want actOld to be a cell array.
Ah yes, for some reason I read the error message the wrong way round, converting from cell to double!
I want it to be an array because if I used actOld{i} there will be an error in line
[~,nUsers(k)]= length(find(act==k))
the error message "Undefined function 'eq' for input arguments of type 'cell'."
I could give you (what I think is) a solution for that, but that would just lead to you getting stuck somewhere else. Trying to guess what you are trying to achieve is wasting your and my time.
Instead of blindly forging ahead, I'd recommend you read the documentation on cell arrays so you understand what you are doing and see if there are better ways.
Maybe somebody else will, but for me, going through your code and fixing it for you is the definition of work. People tend to get paid for that.
Not offering my services, just ranting.
Well I don't see how it is a cell array at the moment. It appears to be a numeric array that is sub-optimally growing in a loop.

Sign in to comment.

Answers (2)

Your variable actOld must be cell. Check it using class(actOld);
>> actOld=cell(5,1);
>> actOld(1)=randi(3)
Conversion to cell from double is not possible.
Use actOld{i}=randi(i) if that is what you want.

3 Comments

I want it to be an array because if I used actOld{i} then act(j) will be act{j} and there will be an error in line
[~,nUsers(k)]= length(find(act==k))
the error message "Undefined function 'eq' for input arguments of type 'cell'."
If you need to be able to assign an entire vector to act(j) where j is a scalar, then you will need to create a new Vector object class and define appropriate indexing and mathematical operations for it. When you have act(j) with scalar j then that is notation for referring to a scalar.
If I recall correctly, someone put a vector class into the File Exchange.
find(act==k)
As you want act to be a vector of vectors, and as k is a scalar there will be no places where the vector act(j) is equal to the scalar k.

Sign in to comment.

You do not need a vector class or a cell array. The problem can be solved with numerical arrays directly:
uAve = zeros(nRoads, nPlayers);
actOld = randi(3, 3, nPlayers);
No need for a loop. Accessing the sub matrices works easy and efficient. So there is potential to simplify the code and fix the problem at the same time.

Categories

Asked:

on 29 Aug 2017

Commented:

on 30 Aug 2017

Community Treasure Hunt

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

Start Hunting!