Elementwise addition operation in matrix or array

77 visualizaciones (últimos 30 días)
Sanket Raval
Sanket Raval el 10 de Feb. de 2022
Comentada: Sanket Raval el 11 de Feb. de 2022
BD is 33*3 data.
BD=[ 1 0 0
2 100 60
3 90 40
4 120 80
5 60 30
6 60 20
7 200 100
8 200 100
9 60 20
10 60 20
11 45 30
12 60 35
13 60 35
14 120 80
15 60 10
16 60 20
17 60 20
18 90 40
19 90 40
20 90 40
21 90 40
22 90 40
23 90 50
24 420 200
25 420 200
26 60 25
27 60 25
28 60 20
29 120 70
30 200 600
31 150 70
32 210 100
33 60 40];
BD(:,2)
ans = 33×1
0 100 90 120 60 60 200 200 60 60
I want to add data element wise in 2nd column and replace the data in BD.
Suppose addtion of 100 in BD (2,2) should update BD(2,2) to 200. Remaining column data should be as it is.
P(2)=100
BD(:,2) = BD (:,2)+_________
  1 comentario
Sanket Raval
Sanket Raval el 10 de Feb. de 2022
If I use scalar then scalar will be added to all the column value.
P(i) should be added to just in BD(i,2).

Iniciar sesión para comentar.

Respuestas (1)

David Hill
David Hill el 10 de Feb. de 2022
If you are just adding a scalar to column 2 of BD
BD(:,2)=BD(:,2) + Scalar;
  2 comentarios
David Hill
David Hill el 10 de Feb. de 2022
If P is the same length as B, then
BD(:,2)=BD(:,2)+P;
If P is a different length
m=min(size(B,1),length(P));
BD(1:m,2)=BD(1:m,2)+P(1:m);
Sanket Raval
Sanket Raval el 11 de Feb. de 2022
I updated your code:
Solution:1
P(2)=100;
m=min(size(BD,1),length(P));
BD(m,2)=BD(m,2)+P(1,m);
BD
Solution: 2
P=zeros(size(BD,1),1);
P(2)=100;
BD(:,2)=BD(:,2)+P;
BD
Thank you for idea.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by