Modify a cell array and obtain a new cell array

3 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 15 Oct 2019
Edited: Stephen23 on 15 Oct 2019
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 Comments
Stephen23
Stephen23 on 15 Oct 2019
Edited: Stephen23 on 15 Oct 2019
I don't see why you need a cumulative sum to get that result:
GGG = {[1,2,1,2,1,1;5,10,15,20,25,30];[1,1,1,1,1,2,2;7,10,15,20,25,30,35]};
GGG1 = [1,2,1,2,1,2;5,10,15,20,25,30];
for k = 1:numel(GGG)
AAB = GGG{k};
SS{k} = [GGG1(1,:),AAB(1,:);GGG1(2,:),GGG1(2,end)+AAB(2,:)];
end
Giving:
>> SS{1}
ans =
1 2 1 2 1 2 1 2 1 2 1 1
5 10 15 20 25 30 35 40 45 50 55 60
>> SS{2}
ans =
1 2 1 2 1 2 1 1 1 1 1 2 2
5 10 15 20 25 30 37 40 45 50 55 60 65
luca
luca on 15 Oct 2019
Edited: luca on 15 Oct 2019
Thanks for the patience ! It's works now! this is really better

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!