Reducing every value of a matrice every time by the same value

조회 수: 1 (최근 30일)
gamer
gamer 2021년 6월 10일
댓글: gamer 2021년 6월 12일
Hello, I am new at matlab and I have a question. Is it possible to reduce every single value of a nx2 matrix by the same amount until all values are zero? Maybe in a for loop?
  댓글 수: 3
dpb
dpb 2021년 6월 10일
Of course. Math operations by constants on arrays work on the entire array by default.
Of course, hitting zero exactly would require a specific value that is evenly divisible into the original value without rounding so it may or may not work out exactly depending upon the values in the array.
Or, of course, if the values in the array aren't the same, you'd have to compute that difference by dividing by the number of iterations and then do array addition/subtraction which is also array-wide.
Again, floating point rounding can make hitting exactly zero tricky...
John D'Errico
John D'Errico 2021년 6월 11일
Please don't keep on asking the same question. If you want more about this question, then make a comment to Matt's answer, asking him for clarification.

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

답변 (1개)

Matt J
Matt J 2021년 6월 10일
편집: Matt J 2021년 6월 10일
This might be what you want, but as @Scott MacKenzie says you've left a lot of problem details and issues for us to guess,
while any(matrix(:)>0)
matrix=max(matrix-something,0);
end
  댓글 수: 4
dpb
dpb 2021년 6월 11일
Oh. I see I just duplicated Matt's answer that was already there...I had just read the comments before.
gamer
gamer 2021년 6월 12일
So first thank you very much for your answers Matt and dpb. Its really that you want to help me!
I tried that one but I think I have to explain the problem more in detail. I created n spheres with that starting location.
p=[r + (a-2*r)*rand(n,1),r + (b-2*r)*rand(n,1)];
Then I wanted to move this spheres. So I created a matrix for speed.
v=rand(n,2);
Then I want to update the position with a for loop.
for f = 1:1000
p = p + v;
end
The movement is working. But I dont get how to slow the balls down. Is it possible to create f = 1000 of matrix out of the first one and the values are always reduced be a=0.001. And if one value is smaller than 0.001, this value should be 0 for the following matrixs
So the for loop would be: p = p + v(f)?

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

카테고리

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