Insert value at particular index of cell array in a loop

22 ビュー (過去 30 日間)
Samia Mahmood
Samia Mahmood 2020 年 8 月 12 日
回答済み: KSSV 2020 年 8 月 12 日
I want to create an cell array of unequal length in matlab.I create an empty cell and add values like
temp = {[1,2,3,21,23]};{[11,2,10]};{1};
just like hash tables having unequal length of linked list while calculating values in a loop.
For my problem statement i want to create a new row matrix for every new set of values .For example i got r= 23,i put that in a matrix next time
r= 40 i create new matrix for all values lying within ht e range of +,- 1.
Since, we cant create run time new matrix of unequal length.
As in this case i can get
c = [1,1.5,1.6,1.7];[23,22,21];[40,41]
Due to unequal length ,matrix cant solve this issue.cell array can but how to insert value at matrix of range 40.

回答 (1 件)

KSSV
KSSV 2020 年 8 月 12 日
You can insert a number in an array on knowing the value of the element and at what position you want to insert the value.
A = [1 2 0 6 7 1] ;
val = 0.5 ; % insert this value at indiex idx
idx = 2 ; % index where to insert the value val
B = [A(1:idx-1) val A(idx:end)] ;
Now you can run a loop for each cell array using the above procedure to insert values.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by