looking for multiple values in a matrix

1 次查看(过去 30 天)
Hi guys
I created a code. The purpose of this code is:
It goes to the first row of matrix birfazson. It looks where it says "1" in the first row.
For example, if 1 is written in the fourth, eighth, tenth columns, in the new_m matrix, it writes:
birfazson :
2 7 5 1 6 3 2 1 9 1
5 7 1 4 8 6 9 1 5 1
new_m :
4
8
10
Then I did sum, cumsum etc.
There is something I want to do now.
We performed all these operations by searching for the value 1 in the phaseson matrix.
But I need to perform these operations for the values 2,3,4,5,6,7,8,9,10.
that is, one should look at the value of 2 in the phaseson matrix, write it down somewhere, and perform the sum operation on it as well.
My code is :
result = birfazson(1,birfazson(2,:) == 1).'
new_m = [result birincisure(1,result).']
proctime=[birincisure(1,result).'] ;
Yy = zeros(numel(proctime),3);
Yy(:,1) = proctime(:,1);
Xx = cumsum(Yy(:,1));
Yy(2:end,2) = Xx(1:end-1);
Yy(:,3) = Xx(:);
Do I need to use a for loop to do this? How can I do it?
(My matrices are pretty big, I used small matrices for example)
Please let me know if I wasn't descriptive enough.
(NOTE :This is how I did it, the code became long and complex like this. )
result = birfazson(1,birfazson(2,:) == 1).'
new_m = [result birincisure(1,result).']
proctime=[birincisure(1,result).'] ;
suretoplam = zeros(numel(proctime),3);
suretoplam(:,1) = proctime(:,1);
Xx = cumsum(suretoplam(:,1));
suretoplam(2:end,2) = Xx(1:end-1);
suretoplam(:,3) = Xx(:);
suretoplam_new=[new_m(:,1) suretoplam]
result1 = birfazson(1,birfazson(2,:) == 2).'
new_m1 = [result1 birincisure(1,result1).']
proctime1=[birincisure(1,result1).'] ;
suretoplam1 = zeros(numel(proctime1),3);
suretoplam1(:,1) = proctime1(:,1);
Xx1 = cumsum(suretoplam1(:,1));
suretoplam1(2:end,2) = Xx1(1:end-1);
suretoplam1(:,3) = Xx1(:);
suretoplam_new1=[new_m1(:,1) suretoplam1]
result2 = birfazson(1,birfazson(2,:) == 3).'
new_m2 = [result2 birincisure(1,result2).']
proctime2=[birincisure(1,result2).'] ;
suretoplam2 = zeros(numel(proctime2),3);
suretoplam2(:,1) = proctime2(:,1);
Xx2 = cumsum(suretoplam2(:,1));
suretoplam2(2:end,2) = Xx2(1:end-1);
suretoplam2(:,3) = Xx2(:);
suretoplam_new2=[new_m2(:,1) suretoplam2]
result3 = birfazson(1,birfazson(2,:) == 4).'
new_m3 = [result3 birincisure(1,result3).']
proctime3=[birincisure(1,result3).'] ;
suretoplam3 = zeros(numel(proctime3),3);
suretoplam3(:,1) = proctime3(:,1);
Xx3 = cumsum(suretoplam3(:,1));
suretoplam3(2:end,2) = Xx3(1:end-1);
suretoplam3(:,3) = Xx3(:);
suretoplam_new3=[new_m3(:,1) suretoplam3]
result4 = birfazson(1,birfazson(2,:) == 5).'
new_m4= [result4 birincisure(1,result4).']
proctime4=[birincisure(1,result4).'] ;
suretoplam4 = zeros(numel(proctime4),3);
suretoplam4(:,1) = proctime4(:,1);
Xx4 = cumsum(suretoplam4(:,1));
suretoplam4(2:end,2) = Xx4(1:end-1);
suretoplam4(:,3) = Xx4(:);
suretoplam_new4=[new_m4(:,1) suretoplam4]
result5 = birfazson(1,birfazson(2,:) == 6).'
new_m5= [result5 birincisure(1,result5).']
proctime5=[birincisure(1,result5).'] ;
suretoplam5 = zeros(numel(proctime5),3);
suretoplam5(:,1) = proctime5(:,1);
Xx5 = cumsum(suretoplam5(:,1));
suretoplam5(2:end,2) = Xx5(1:end-1);
suretoplam5(:,3) = Xx5(:);
suretoplam_new5=[new_m5(:,1) suretoplam5]
result6 = birfazson(1,birfazson(2,:) == 7).'
new_m6= [result6 birincisure(1,result6).']
proctime6=[birincisure(1,result6).'] ;
suretoplam6 = zeros(numel(proctime6),3);
suretoplam6(:,1) = proctime6(:,1);
Xx6 = cumsum(suretoplam6(:,1));
suretoplam6(2:end,2) = Xx6(1:end-1);
suretoplam6(:,3) = Xx6(:);
suretoplam_new6=[new_m6(:,1) suretoplam6]
result7 = birfazson(1,birfazson(2,:) == 8).'
new_m7= [result7 birincisure(1,result7).']
proctime7=[birincisure(1,result7).'] ;
suretoplam7 = zeros(numel(proctime7),3);
suretoplam7(:,1) = proctime7(:,1);
Xx7 = cumsum(suretoplam7(:,1));
suretoplam7(2:end,2) = Xx7(1:end-1);
suretoplam7(:,3) = Xx7(:);
suretoplam_new7=[new_m7(:,1) suretoplam7]
result8 = birfazson(1,birfazson(2,:) == 9).'
new_m8= [result8 birincisure(1,result8).']
proctime8=[birincisure(1,result8).'] ;
suretoplam8 = zeros(numel(proctime8),3);
suretoplam8(:,1) = proctime8(:,1);
Xx8 = cumsum(suretoplam8(:,1));
suretoplam8(2:end,2) = Xx8(1:end-1);
suretoplam8(:,3) = Xx8(:);
suretoplam_new8=[new_m8(:,1) suretoplam8]
result9 = birfazson(1,birfazson(2,:) == 10).'
new_m9= [result9 birincisure(1,result9).']
proctime9=[birincisure(1,result9).'] ;
suretoplam9 = zeros(numel(proctime9),3);
suretoplam9(:,1) = proctime9(:,1);
Xx9 = cumsum(suretoplam9(:,1));
suretoplam9(2:end,2) = Xx9(1:end-1);
suretoplam9(:,3) = Xx9(:);
suretoplam_new9=[new_m9(:,1) suretoplam9]
Thank u for help.
  2 个评论
Jan
Jan 2022-4-4
编辑:Jan 2022-4-4
Just a hint: Renaming the matrix from "birfazson" to "X" or "A" would increase the readability.
Using a loop seems to be a good idea. Maybe:
Yy = zeros(numel(proctime), 3, 8);
for k = 1:8
result = A(1, A(2,:) == k);
new_m = [result B(1, result).'];
proctime = C(1, result);
Xx = cumsum(Yy(:,1));
Yy(:, 1, k) = proctime(:, 1);
Yy(2:end, 2, k) = Xx(1:end-1);
Yy(:, 3, k) = Xx(:);
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by