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

3 ビュー (過去 30 日間)
Nora Khaled
Nora Khaled 2017 年 8 月 29 日
コメント済み: Nora Khaled 2017 年 8 月 30 日
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 件のコメント
Adam
Adam 2017 年 8 月 29 日
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.
Nora Khaled
Nora Khaled 2017 年 8 月 30 日
thank you all.

サインインしてコメントする。

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2017 年 8 月 29 日
編集済み: Fangjun Jiang 2017 年 8 月 29 日
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 8 月 29 日
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.
Nora Khaled
Nora Khaled 2017 年 8 月 30 日
thank you all.

サインインしてコメントする。


Jan
Jan 2017 年 8 月 29 日
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.

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by