Manipulating cell arrays based on index
古いコメントを表示
how can I manipulate the elements of an array of cells based on index? for a double array, I can just do this:
>> j = zeros(4);
>> j(1:8) = 1;
>> j(9:16) = 8;
>> j
j =
1 1 8 8
1 1 8 8
1 1 8 8
1 1 8 8
However, for a cell array, I cannot
>> j = cell(4,1)
j =
[]
[]
[]
[]
>> j{1:4} = [2 3 4]
The right hand side of this assignment has too few values to satisfy
the left hand side.
I could do this with a for loop easily, but I intend to extend this to a cell with ~400 elements.
After this is figured out, I'd also like to be able to do something based on the index, such as:
j = cell(100,1);
j(1:100) = ones(1:100,1);
1 件のコメント
You really need to read the documentation for ones, because providing it with a vector does not make any sense... it does not do what you think it does.
Also you should not use i or j for variable names, as these are both names of the inbuilt imaginary unit.
採用された回答
その他の回答 (1 件)
Azzi Abdelmalek
2015 年 7 月 1 日
編集済み: Azzi Abdelmalek
2015 年 7 月 1 日
jj=cell(100,1)
jj(1:100) = num2cell(ones(100,1))
3 件のコメント
Noah Chrein
2015 年 7 月 1 日
Azzi Abdelmalek
2015 年 7 月 1 日
編集済み: Azzi Abdelmalek
2015 年 7 月 1 日
jj=cell(100,1);
out=cellfun(@(x) ones(100,1),jj,'un',0)
Noah Chrein
2015 年 7 月 1 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!