How to remove values from an array which do not create output?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 9 May 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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!