Modify a cell array and obtain a new cell array

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?

 采纳的回答

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 个评论

Thanks Stephen you are right,
But now if I write
for i= 1:numel(GGG)
AAAA{i}=[GGG1(1,:),GGG{i}(1,:);GGG1(2,end)+cumsum(diff([0,GGG{i}(2,:)]))];
end
How I can modified the code to get the result? Maybe creating a for loop and analyze once a time GGG. I cannot find a way to bypass this problem.
Is there a way to obtain what I want?
"How I can modified the code to get the result?"
You need to consider that:
  1. GGG1(1,:) has exactly the same number of columns on every iteration (34)
  2. GGG{i} has different numbers of columns (1st iteration 34, 2nd iteration 35).
As you do not explain how you want to vertically concatenate a 34-element vector with a 35-element vector, I simply don't know what your expected "result" is.
"I cannot find a way to bypass this problem"
The current problem appears to be that you want to concatenate vectors with incompatible sizes. The solution to that is simple: do not concatenate vectors with incompatible sizes. If you want to vertically concatenate two row vectors then they must have the same number of elements.
It is not clear what you expect the code in your comment to do, but I very much doubt that making apparently random changes to your code will help you:
If I write this
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 i= 1:numel(GGG)
AAB=GGG{i}
AA=[GGG1(1,:),AAB(1,:);GGG1(2,:),AAB(2,:)];
SS{i}=AA
end
I obtain what I want in SS. but the porblem is that I dont have the cumalative sum in the second row. Consider the first cell of GGG, in SS I get
+
But what I want is the cumulative sum of the difference as soon as I've concatened the second array of GGG to GG1. That I was tryng to do with
cumsum(diff([0,GGG{i}(2,:)]))
What I want to get is
SS{1,1}= [1 2 1 2 1 2 1 2 1 2 1 1; 5 10 15 20 25 30 35 40 45 50 55 60 ];
So, How can I write that cumulative sum to avoid error. Maybe doing cumulative later?
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
Thanks for the patience ! It's works now! this is really better

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品

版本

R2019b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by