For loop question with matrix

I have a matrix as below called A (60x3 double)
A = [10 3.5 -34.66
9 3.5 -35.6
8 3.5 -31.43
7 3.5 -29.04
6 3.5 -27.81
5 3.5 -26.59
4 3.5 -23.69
3 3.5 -16.47
2 3.5 2.94
1 3.5 24.03
5 3.5 25.86
4 3.5 35.98
3 3.5 55.89
2 3.5 91.82
1 3.5 101.5
6 3.5 -63.5
5 3.5 -67.03
4 3.5 -72.58
3 3.5 -80.98
2 3.5 -85.91
1 3.5 -63.15
7 3.5 -20.23
6 3.5 -19.92
5 3.5 -20.05
4 3.5 -19.69
3 3.5 -13.96
2 3.5 11.57
1 3.5 30.76
3 3.5 63.36
2 3.5 99.82
1 3.5 107.13
10 3.5 -47.09
9 3.5 -61.25
8 3.5 -61.89
7 3.5 -63.07
6 3.5 -65.29
5 3.5 -69.63
4 3.5 -76.96
3 3.5 -84.92
2 3.5 -80.39
1 3.5 -57.89]
I want to sum column 2 values until column 1 value is 1. I mean first sum must be on row 1 to row 10, and 2nd sum must be on row 15 to row 20, 3rd sum must be on row 21 to row 26 etc.
How can i do that with using for loop or another way?

1 Comment

The number 1 is in the following rows of column 1:
>> find(A(:,1)==1)
ans =
10
15
21
28
31
41
so I'm having trouble figuring out what rule you're using to select row numbers.

Sign in to comment.

Answers (2)

locs = [find(A(:,1)==1); size(A,1)+1];
tsum = cumsum([0;A(:,2)]);
output = tsum(locs(2:end)) - tsum(locs(1:end-1));

10 Comments

Gko K
Gko K on 23 Mar 2019
Edited: Gko K on 23 Mar 2019
Thank you friend.
I want to do something like this
a=[10 3 -3.04
9 3 3.4
8 3 11.5
7 3 16.5
6 3 20.45
5 3 25.86
4 3 35.98
3 3 55.89
2 3 91.82
1 3 101.5]
b=a(:,1);
c=a(:,2);
vd=a(:,3);
ht=sum(c);
for i=1:length(b)
if b(i)==1
h(i)=ht;
else
h(i)=ht-b(i+1)*c(i);
end
end
h=h'
hw3=ht/3
hwt3=ht-hw3
vv=max(vd)
vv05=vv/2
for ii=1:length(vd)
if vd(ii)<vv05
v(ii)=vv05
elseif vd(ii)>=vv05
v(ii)=vd(ii)
end
end
v=v'
for j=1:length(b)
if h(j)>=hwt3 && v(j)>=0.5*vv
ve(j)=v(j);
elseif h(j)>=hwt3 && v(j)<0.5*vv
ve(j)=0.5*vv;
elseif h(j)<hwt3 && v(j)>=0.5*vv
ve(j)=v(j);
%%ve(j)=interp1(h(j),v(j),h(j));%%
elseif h(j)<hwt3 && v(j)<0.5*vv
ve(j)=0.5*vv;
end
end
ve=ve'
here is only one matrix. but i have a bigger matrix as below
a= [10 3.5 -34.66
9 3.5 -35.6
8 3.5 -31.43
7 3.5 -29.04
6 3.5 -27.81
5 3.5 -26.59
4 3.5 -23.69
3 3.5 -16.47
2 3.5 2.94
1 3.5 24.03
5 3.5 25.86
4 3.5 35.98
3 3.5 55.89
2 3.5 91.82
1 3.5 101.5
6 3.5 -63.5
5 3.5 -67.03
4 3.5 -72.58
3 3.5 -80.98
2 3.5 -85.91
1 3.5 -63.15
7 3.5 -20.23
6 3.5 -19.92
5 3.5 -20.05
4 3.5 -19.69
3 3.5 -13.96
2 3.5 11.57
1 3.5 30.76
3 3.5 63.36
2 3.5 99.82
1 3.5 107.13
10 3.5 -47.09
9 3.5 -61.25
8 3.5 -61.89
7 3.5 -63.07
6 3.5 -65.29
5 3.5 -69.63
4 3.5 -76.96
3 3.5 -84.92
2 3.5 -80.39
1 3.5 -57.89]
I want to run my code until i reach the value=1 on column 1. After this point i want to run again until i reach the value=1.......
In this big matrix i should have 6 matrices to run the code. How can i do that
G = cumsum(a(:,1)==1) + 1;
results = splitapply(@YourFunction, a, G)
YourFunction will be passed a subset of a to process. The first subset will be the portion before the first 1; if it is important that that subset not be processed, then it would not be difficult to skip it.
I get en error like this
Error using splitapply (line 132)
Applying the function 'YourFunction' to the 1st group of data generated the following
error:
Attempt to execute SCRIPT YourFunction as a function:
I saved my code as YourFunction.m file.
How can i solve problem?
Rewrite your code so that it can take as input a subsection of a matrix that has already been split, and calculates the appropriate values, and returns them as a vector or as a cell array.
splitapply will put all of the results together into a single array, one row per group.
Thank you friend
I am a beginner about matlab.
I cant do that. Can you help me to do it?
function result = YourFunction(subset)
b = subset(:,1);
c = subset(:,2);
vd = subset(:,3);
a bunch of statements here
result = {v, ve}; %or whatever you want to remember about the subset
end
Thank you for your all help.But i cant solve my problem with my little knowledge.
Sorry if i made you busy.
İf you have a spare time, can you solve for me?
No, your code is not well enough commented for me to be able to figure out what you want to compute.
If you were given one subset, that began with a "1" and ended just before the next "1", then what outputs would you calculate from it?
Yeah, that was unclear to me from the beginning.
Gko K
Gko K on 24 Mar 2019
Edited: Gko K on 24 Mar 2019
Ok, thank you both, Sorry if i make you busy.
I have analysis results which are in a big excel table.
I want to split matrix as i show below:
My code runs true if i have 1 matrix. But i have a big table as shown in the pic.I want to run code for matrix 1 then matrix 2 then matrix 3 then matrix 4 then matrix 5 then matrix....n.
If you dont want to help no problem. Thanks for your previous helps friends.

Sign in to comment.

Adam Danz
Adam Danz on 24 Mar 2019
Edited: Adam Danz on 24 Mar 2019
"I want to sum column 2 values until column 1 value is 1."
With your input matrix "A", 's' is the sum of column 2 for each group.
% Create group numbers for each row
rowGroups = cumsum([0;A(1:end-1,1)] == 1)+1;
% Calculate sum of col 2 for each group
s = splitapply(@sum, A(:,2), rowGroups)

5 Comments

Thank you friend,
That can work but i must try to make it true for my code.
Here is my code for one matrix.
a=[10 3 -3.04
9 3 3.4
8 3 11.5
7 3 16.5
6 3 20.45
5 3 25.86
4 3 35.98
3 3 55.89
2 3 91.82
1 3 101.5]
b=a(:,1);
c=a(:,2);
vd=a(:,3);
ht=sum(c);
for i=1:length(b)
if b(i)==1
h(i)=ht;
else
h(i)=ht-b(i+1)*c(i);
end
end
h=h'
hw3=ht/3
hwt3=ht-hw3
vv=max(vd)
vv05=vv/2
for ii=1:length(vd)
if vd(ii)<vv05
v(ii)=vv05
elseif vd(ii)>=vv05
v(ii)=vd(ii)
end
end
v=v'
for j=1:length(b)
if h(j)>=hwt3 && v(j)>=0.5*vv
ve(j)=v(j);
elseif h(j)>=hwt3 && v(j)<0.5*vv
ve(j)=0.5*vv;
elseif h(j)<hwt3 && v(j)>=0.5*vv
ve(j)=v(j);
%%ve(j)=interp1(h(j),v(j),h(j));%%
elseif h(j)<hwt3 && v(j)<0.5*vv
ve(j)=0.5*vv;
end
end
ve=ve'
Thank you, i will try your way to solve my problem
Adam Danz
Adam Danz on 24 Mar 2019
Edited: Adam Danz on 24 Mar 2019
I have a feeling most of your code can be replaced by a few lines of code. The code you shared is hard to follow for the following reasons.
  • It's not properly indented. If you select all of your code in matlab editor and then press ctrl+i, your code will be properly indented and much easier to read.
  • You have redundant variables. Instead of doing this: b=a(:,1), just use a(:,1) whenever you're useing 'b'. There's no need to take apart the 'a' matrix and reassign each column to a new variable. Likewise, "length(b)" can be "size(a,1)".
  • Meaningless variable names. hwt3, vv, c, vd, etc. The only way to understand what those variables are is to deconstruct the entire code. Instead, use variable names that are meaningful. For example, instead of "ht=sum(c)", you could use "sumCol2 = sum(a(:,2))". Instead of "vv=max(vd)", you could use "maxCol3 = max(a(:,3))"
  • Use comments. Explain the steps you're taking by inserting %comments above or to the right of each section of code.
Ok friend, i will try this.
Sorry if i make you busy and if i ask very simple questions. I am not good at matlab and programming.
We've all been there.
Thank you

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2018b

Tags

Asked:

on 23 Mar 2019

Commented:

on 25 Mar 2019

Community Treasure Hunt

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

Start Hunting!