Adding values of the matrix
古いコメントを表示
Hello, I would like to add the elements which are passing through this line A1,B1 & A2,B2 .

1) the result of A1,B1 should be 1+5+5+1+4= 16
2) the reulst of A2,B2 should be 2+2+2+4= 10
Asuume we do not know which blocks it crosses exactly , hence we need to check for all values of x and y
Working till now.
1) since i do not know the locations the line crosses the blocks but i know A1(2,1) and B1(4,6) . I fould the slope of the line, then ran a loop x = 1:6 and y = 1:6 to find out which combinations of x&y have the same slope of this line, if it is the same then i added the value of (x,y) (eg(3,2) = 5 ) to my Sum.
2) i know that the line may not pass through the center so i also used the round(x),round(y) function to get the approximate values.
My question is, is there a better / easier method to do this ? I need to do this for 30*30*30*30 lines. I basically want to find the sum of the values between two points of a line drawn across it.
Thank You
3 件のコメント
dpb
2020 年 12 月 12 日
1) the result of A1,B1 should be 1+5+5+1+4= 16"
Why not +9?
Praseetha Puducode Narayanan
2020 年 12 月 12 日
dpb
2020 年 12 月 12 日
Your drawing shows A1:B1 to cross the cell containing 9 on the RH end; you pick up both the 1 & 5 at the LH end, why not both 4 & 9 on RH?
Similar argument for RH end of A2:B2 with cell containing value 3.
Logic based on distance and crossing geometries would find those as well; to remove would special-case them
回答 (1 件)
Cris LaPierre
2020 年 12 月 12 日
0 投票
Sounds like a nice assignment to teach you how to use nested for loops and an if statement with two conditionals. You can learn more about each of these programming constructs in Ch 13 of MATLAB Onramp.
カテゴリ
ヘルプ センター および File Exchange で Image Arithmetic についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!