How to remove values from an array which do not create output?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
A. B. M. Mesbah Uddin Ahmed
el 9 de Mayo de 2021
Respondida: Walter Roberson
el 9 de Mayo de 2021
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.
0 comentarios
Respuestas (1)
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)
surf(A,B,C)
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!