Read through header number and add correponding columns from different arrays

조회 수: 1 (최근 30일)
Damith
Damith 2014년 6월 10일
편집: Damith 2014년 6월 10일
Hi,
I have following arrays.
H=[438 440 441 449 455 457 459 460 462 467 468 469 473 476 478 479 481 483 485 486 487];
All_St1=[440 449 457 462 467;
rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1)];
All_St2=[438 441 462 468 473 479;
rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1)];
All_St3=[455 478 479 481 483 485 486;
rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1) rand(100,1)];
I need to check the header numbers from H, first row of All_St1, All_St2 and All_St3. Then if the header numbers are matching/similar from All_St1, 2 and 3 then sum/add the corresponding columns from row 2:end.
For example, under header number 462, All_St1 4th column and All_St2 3rd columns shoud be summed/added.
Output matrix (Out) should have 101 rows the header numbers H. If there is no header number of H in All_St1,2,and 3, the corresponding column should be empty/zero.
Can somebody help me to implement this in MATLAB?
Thanks in advance.

답변 (1개)

Image Analyst
Image Analyst 2014년 6월 10일
Did you try a nested for loop? It's very simple/basic. A brute force method but should be very fast especially for arrays this small. Come on, give it a try. I know you can do it if you just make an attempt because a for loop is about the very first thing you learn in programming. Here, I'll get you started...
for hk = 1 : length(H)
hValue = H(hk)
for hs = 1 : size(All_St1, 2)
value1 = All_St1(1, hs);
value2 = All_St2(1, hs);
value3 = All_St3(1, hs);
if ........
end
end
See if you can finish it.
  댓글 수: 1
Damith
Damith 2014년 6월 10일
편집: Damith 2014년 6월 10일
Hey Thanks. But did you see size(All_St1,2 and 3) are diffrent? How can I check for header numbers for All_St's. If your example you check for first 5. But All_St2 and 3 have more columns.
How can I find the similar row numbers from value1, value2 and value3? Any thought on that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by