How to plot a sphere with given centre and radius?

There is a matrix A=[1 2 3 4;2 3 4 5;5 6 7 8;8 9 10 11], First three columms represents the centre of a sphere (a,b,c), and the last columm represents the radius. How to plot those spheres in a same figure? Try not to use loops. Thanks.

2 件のコメント

Jan
Jan 2017 年 9 月 28 日
Until "try not to use loops" the question sounds seriously. The solution is trivial with the sphere command and a loop. A solution without a loop is neither smart nor efficient.
Nicolson
Nicolson 2017 年 9 月 28 日
Now I already have my code and it is working perfectly ;) with loop and hold on, pretty easy.

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 9 月 28 日
編集済み: Jan 2017 年 9 月 28 日

1 投票

Really without a loop? This is a bad idea for something, which is definitely a job for a loop.
figure
axes('NextPlot', 'Add');
CMap = parula(4);
A = [1 2 3 4; 2 3 4 5; 5 6 7 8; 8 9 10 11];
[X, Y, Z] = sphere;
for k = 1:4
XX = X * A(k, 4) + A(k, 1);
YY = Y * A(k, 4) + A(k, 2);
ZZ = Z * A(k, 4) + A(k, 3);
surface(XX, YY, ZZ, 'FaceAlpha', 0.5, 'FaceColor', CMap(k, :), ...
'EdgeColor', [0.4, 0.4, 0.4]);
end
axis tight
view(3)
No, I tried it without a loop but it hurts my eyes. Here is a loop version.
I made the spheres semi-transparent, because they overlap.

1 件のコメント

Nicolson
Nicolson 2017 年 9 月 28 日
Thank you :) I used surf function and the final result is same.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 9 月 28 日

編集済み:

Jan
2017 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by