How to perform operation on sepcific elements in an array?

조회 수: 6 (최근 30일)
Rabia Zulfiqar
Rabia Zulfiqar 2020년 6월 6일
편집: KSSV 2020년 6월 6일
I have an array 'A' of size 1×365. There is an another array 'B' consisting of linear indexing of size 1×160. Now I want to create another array 'C' of size 1×365 where I have to perform operation on the elements according to the index of B. For example A=[2 4 6 8 10] B=[1 4] I want to perform A-2.5 on the elements of A to create a new matrix C and convert the remaining elements to zero in A. The answer will be C=[-0.5 0 0 5.5 0] As B(1)=1 and B(2)=4 so the mathematical operation is performed only on those elements and the remaining elements are converted to zero. How Can I do this? Thankyou in advance.

답변 (1개)

KSSV
KSSV 2020년 6월 6일
편집: KSSV 2020년 6월 6일
C = zeros(size(A)) ;
C(B) = A(B)-2.5

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by