How to write a for loop code with an undetermined value
조회 수: 1 (최근 30일)
이전 댓글 표시
I am stuck while writing this code. Could you give any advise? The matrix N might consist of 16 different values at maximum(0-16), depending on the previous codes.
x=-20:5:20;
y=-20:5:20;
z=40:5:80;
[X,Y,Z]=meshgrid(x,y,z)
h=0;
for i=1:1:length(x)
for j=1:1:length(y)
for k=1:1:length(z)
h=h+1
M(h,:)=[X(i,j,k), Y(i,j,k), Z(i,j,k)];
% N(h)=a_MATLAB_function([M(h,1); M(h,2); M(h,3) ],[0;0;0]);
end
end
end
N=[12 12 12 12 12 12 12 8 4 4 4 12 12 ]; % Matrix N's size is 1x729 actually.
% Here it is simplified as a shorter row.
NN = unique(N); NN_length=length(NN);
S=[35 50 100 200];
figure; hold on;
for i=1:1:NN_length
NNN(i,:)=find(N==NN(i))
scatter3(M(NNN(i,:),1),M(NNN(i,:),2),M(NNN(i,:),3),S(i))
end
hold off;
For the first value of i=1, it is fine. But later I receive error. Because while writing the second row of NNN, first row and second row dimension doesn't match. What can I do? Thanks in advance.
댓글 수: 0
채택된 답변
Jan
2021년 2월 7일
편집: Jan
2021년 2월 7일
Is there a reason to collect NNN in an array?
for i = 1:NN_length
NNN = (N == NN(i)); % Without FIND: faster logical indexing
scatter3(M(NNN, 1), M(NNN, 2), M(NNN, 3), S(i))
end
If you do need NNN later on:
NNN = cell(1, NN_length)
for i = 1:NN_length
NNN{i} = (N == NN(i)); % Without FIND: faster logical indexing
scatter3(M(NNN{i}, 1), M(NNN{i}, 2), M(NNN{i}, 3), S(i))
end
댓글 수: 8
Walter Roberson
2021년 2월 8일
The steps to enable a license to use Mathworks itself as the license server are described at https://www.mathworks.com/help/licensingoncloud/matlab-on-the-cloud.html .
This is available for individual licenses -- Educational or Standard (commercial / professional), but not for Home or Student licenses.
If I recall correctly, the people who offered that old cracked software were arrested for software piracy.
acun67 acu
2021년 2월 9일
thank you for sharing your knowledge.I am not using this installation and will try to solve my problem using Matlab online.
추가 답변 (1개)
David Hill
2021년 2월 7일
What are you trying to plot? What is M?
[n,nn,nnn]=unique(N);%;might want to look at the outputs here, they might help you
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!