MATLAB Answers

Modify a cell array and obtain a new cell array

10 ビュー (過去 30 日間)
luca
luca 2019 年 10 月 15 日
編集済み: luca 2019 年 10 月 15 日
Hi, I want to create a new cell array AAAA with the following code.
GGG= {[1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 6 6 6 6 6 6 3 3 9 9 3 9 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 ],[1 1 1 1 1 2 3 2 2 3 4 4 4 5 5 4 4 5 4 6 3 6 6 3 6 3 6 3 3 9 3 9 9 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175]};
tt = [20 20 20 20 20 20 20 20 20];
GGG1= [1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 6 6 6 6 6 6 3 3 9 9 3 9 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 ];
for i=1:numel(GGG)
AAAA{i}=[GGG1(1,:);GGG1(2,end)+cumsum(diff([0,GGG(2,:)]))];
end
In AAAA I have the saem number of cell of GGG.
Considering just the first cell of GGG.the first raw of AAA is made merging the first raw GGG1 with the first raw of GGG. the second raw of AAA is made by the second raw of GGG1 and then summing each difference (between an element and is preceeding) of the second raw of GGG. As indicated in the formula
cumsum(diff([0,GGG(2,:)]))
I dont know why but I obtain this error:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in Untitled555555 (line 10)
AAAA{i}=[GGG1(1,:);GGG1(2,end)+cumsum(diff([0,GGG(2,:)]))];
Result for the first cell should be :
AAAA= [1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 6 6 6 6 6 6 3 3 9 9 3 9 9 9 1 2 1 2 1 1 1 2 3 4 4 5 4 5 5 4 4 5 5 4 6 6 6 6 6 6 3 3 9 9 3 9 9 9; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 305 310 315 320 325 330 335 340]
could someone help me?
  2 件のコメント
luca
luca 2019 年 10 月 15 日
thanks

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

採用された回答

Stephen
Stephen 2019 年 10 月 15 日
編集済み: Stephen 2019 年 10 月 15 日
The problen is that you try to access a row of GGG which does not exist.
You define GGG as a 1x2 cell array:
GGG= {[...],[...]};
GIven that GGG has size 1x2 (i.e. it only has one row), what do you expect to happen when you try to access its second row (which does not exist)?:
GGG(2,:) % this will throw an error because GGG only has one row
I suspect that you actually wanted to access the second row of the matrix inside the cell:
GGG{i}(2,:)
Note that on the second loop iteration your code will throw another error due to a mismatch in the sizes of the arrays in GGG:
>> size(GGG{1}) % no problem
ans =
2 34
>> size(GGG{2}) % this will throw an error.
ans =
2 35
  5 件のコメント
luca
luca 2019 年 10 月 15 日
Thanks for the patience ! It's works now! this is really better

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

その他の回答 (0 件)

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by