Iterating over values in multiple arrays

조회 수: 6 (최근 30일)
g
g 2020년 3월 17일
댓글: Akira Agata 2020년 3월 18일
Let's say I have an array A defined as
A = [0 2 5 7 8]
Let's say I haven array B defined as
B = linspace[0,10,100]
Now I want to perform an operation on each value in B that is greater than or equal to the previous value in A. For simplicity, let's just say subtraction.
Basically I want to perform the following operations (and store the results as an array):
(0-0)
(.1-0)
(.2-0)
...
(1.9-0)
(2-2)
(2.1-2)
...
(9.8-8)
(10-8)
Because of other code involved, I want to index over the values of A like so
n=length(A);
for j=1:n
x0 = A(j);
% Now I want to subtract each value in B minus the appropriate x0 and save all the results as an array
end
How could I accomplish the rest of the desired procedure?

답변 (1개)

Akira Agata
Akira Agata 2020년 3월 18일
How about the following?
C = B'-A;
C = C(:);
  댓글 수: 2
g
g 2020년 3월 18일
This works for subtraction, but not general operations within the scheme provided.
Akira Agata
Akira Agata 2020년 3월 18일
It's not clear for me.
Could you tell us more details on your problem and what the output should be or looks like?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by