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
dpb 2020년 12월 12일
1) the result of A1,B1 should be 1+5+5+1+4= 16"
Why not +9?
Praseetha Puducode Narayanan
Praseetha Puducode Narayanan 2020년 12월 12일
I want to sum up all the points the line crosses. so the maximum will be 16.
dpb
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
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.

댓글 수: 2

Praseetha Puducode Narayanan
Praseetha Puducode Narayanan 2020년 12월 12일
Thanks for that information. I am currently using nested loops to determine if the points lay on the line, as stated above.
Is there another way to perform the summation than comparing if the points lay on the line ?
Cris LaPierre
Cris LaPierre 2020년 12월 12일
You may want to use round, floor, and/or ceil.

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

댓글:

dpb
2020년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by