Sum of index elements

Hi, I have a matrix A(641x481) with numbers 0<x<3 and others matrix B,C,D and F with the same size of matrix A. I need create a matrix G, but depending the value of the element in A, the value in G will be the sum of different combinations of B,C, D and F. For example
If A(i,j) < 0.25; so G(i,j) = B(i,j) + C(i,j);
If 0.25< A(r,s)<1; so G(r,s) = B(r,s) + D(r,s);
etc
Thanks

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 17 Nov 2014
Edited: Azzi Abdelmalek on 17 Nov 2014
A=rand(4,5);
[n,m]=size(A)
B=rand(n,m)
C=rand(n,m)
D=rand(n,m)
G=zeros(n,m)
idx1=A < 0.25
G(idx1) = B(idx1) + C(idx1)
idx2=0.25<A & A<1;
G(idx2) = B(idx2) + D(idx2)

1 Comment

A=rand(641,481);
[n,m]=size(A);
B=rand(n,m);
C=rand(n,m);
D=rand(n,m);
G=zeros(120686,1);
idx1=A < 0.25;
G(idx1(:)) = B(idx1(:)) + C(idx1(:));
idx2=0.25<A <1;
G(idx2(:)) = B(idx2(:)) + D(idx2(:));

Sign in to comment.

More Answers (1)

cob
cob on 17 Nov 2014

0 votes

Hi Azzi, I did exactly this but my matrix G instead of having 641x481 elements like the older ones, it has 120686X1 elements. I need to preserve the matrix shape to plot a distribution's plot after. Thanks

1 Comment

Please delete this answer, and rewrite your comment by clicking on comment on this answer

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!