How to remove values from an array which do not create output?

1 visualización (últimos 30 días)
Suppose, I have two arrays like below and some conditions:
a=1:10;
b=1:10;
c=[];
for i=1:length(a)
for j=1:length(b)
if(a(i)>5)
if(b(j)>7)
c=[c i*j];
end
end
end
end
plot3(a,b,c);
The plot3 command will show an error as a,b,c are not of equal length. Now I want to remove the values of a and b which do not produce c. What can I do? How can I plot successfully here? Thanks in advance.

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Mayo de 2021
a=1:10;
b=1:10;
[A,B] = ndgrid(a,b);
C = A.*B;
mask = A > 5 & B > 7;
C = C(mask);
A = A(mask);
B = B(mask);
scatter3(A,B,C)
The above is the general case. But since your tests are independent, you could instead
a=1:10;
b=1:10;
A = a(a>5);
B = b(b>7);
C = A.*B(:);
size(A), size(B), size(C)
ans = 1×2
1 5
ans = 1×2
1 3
ans = 1×2
3 5
surf(A,B,C)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by