How to creating 3 nested for loop to obtain 3d matrix?

조회 수: 3 (최근 30일)
Yunus Emre TOMRUK
Yunus Emre TOMRUK 2018년 12월 21일
편집: Yunus Emre TOMRUK 2018년 12월 21일
Hi Everyone,
I am trying to run the following code and obtain a 3d matrix but i got following error.
"Subscript indices must either be real positive integers or logicals.
Error in coilsens (line 13)
B(z,alpha,beta) = z-y*tand(beta)+x*tand(alpha); "
How can i fix this problem?
Thanks.
clc
clear all
u0 = 4*pi*(10e-7);
y = 10e-3;
x = 10e-3;
B = zeros([11,31,31]);
for z = 2e-3:0.1e-3:3e-3
for alpha = 1:0.1:3
for beta = 1:0.1:3
B(z,alpha,beta) = z-y*tand(beta)+x*tand(alpha);
end
end
end

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 12월 21일
편집: Andrei Bobrov 2018년 12월 21일
y = 10e-3;
x = 10e-3;
z = 2e-3:0.1e-3:3e-3;
alpha = 1:0.1:3;
beta = 1:0.1:3;
[zz,alp,bet] = ndgrid(z,alpha,beta);
B = zz-y*tand(bet)+x*tand(alp);
or
y = 10e-3;
x = 10e-3;
z = 2e-3:0.1e-3:3e-3;
alpha = 1:0.1:3;
beta = 1:0.1:3;
B = zz(:)-y*tand(reshape(bet,1,1,[]))+x*tand(alp(:)');
  댓글 수: 3
Luna
Luna 2018년 12월 21일
@Andrei someone please teach me how to use this reshape function right on point everytime like he did!! geniusly avoid for loops amazing!
Yunus Emre TOMRUK
Yunus Emre TOMRUK 2018년 12월 21일
@Andrei Thank you for real quick and great response.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 12월 21일
clc
clear all
u0 = 4*pi*(10e-7);
y = 10e-3;
x = 10e-3;
z = 2e-3:0.1e-3:3e-3;
alpha = 1:0.1:3;
beta = 1:0.1:3;
B=zeros(numel(z),numel(alpha),numel(beta)); % preallocate
for z1=1:numel(z)
for alpha1 = 1:numel(alpha)
for beta1 = 1:numel(beta)
B(z1,alpha1,beta1) = z(z1)-y*tand(beta(beta1))+x*tand(alpha(alpha1));
end
end
end
  댓글 수: 1
Yunus Emre TOMRUK
Yunus Emre TOMRUK 2018년 12월 21일
편집: Yunus Emre TOMRUK 2018년 12월 21일
@Madhan Thank your for the real quick response.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by