How to generate data?

조회 수: 1 (최근 30일)
Ali Najem
Ali Najem 2022년 11월 8일
댓글: Ali Najem 2022년 11월 8일
Dear experts,
I have this code as below:
V = 10660:1:11700;
deltavB = 50:1:70;
VB = 1:1:81
a=V-VB(1); %% VB(2)....VB(81)
b=deltavB/2;
for i =1:length(b)
for j =1:length(a)
c(i,j) = (a(j)./b(i)).^2;
end
end
g = 1./(1+c);
As you can see the size of g is [21 1041] for VB(1)
I need to reapeat this for each value of VB which is 81 points, I suppose to have g [21 1041] for each value of VB
How can I do so and save it as whole data togother?
Thanks in advanced.

채택된 답변

Image Analyst
Image Analyst 2022년 11월 8일
Try this:
V = 10660:1:11700;
deltavB = 50:1:70;
VB = 1:1:81;
all_g = zeros(length(deltavB), length(V), length(VB));
for k = 1 : length(VB)
thisVB = VB(k);
a = V - thisVB; %% VB(2)....VB(81)
b = deltavB / 2;
for i = 1 : length(b)
for j = 1 : length(a)
c(i,j) = (a(j)./b(i)).^2;
end
end
g = 1./(1+c);
% Insert this g into the k'th slice of the 3-D array all_g.
all_g(:, :, k) = g;
end
fprintf('Done!\n')
  댓글 수: 1
Ali Najem
Ali Najem 2022년 11월 8일
Thank you so much for prompt answer.
It worked but all_g in 3-D array and it says "Cannot display summaries of variables with more than 524288 elements"
How do I find or save each [21 1041] for each point from 1 till 81

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 11월 8일
V = 10660:1:11700;
deltavB = 50:1:70;
VB = 1:1:81
numV = length(V);
numdeltavB = length(deltavB);
numVB = length(VB);
g = zeros(numdeltavB, numV, numVB);
b=deltavB/2;
for VBidx = 1 : numVB
a = V - VB(VBidx);
for i = 1:numdeltavB
c(j,:) = (a./b(i)).^2;
end
g(:,:,VBidx) = 1./(1+c);
end
The for i loop could also be eliminated by using automatic expansion. Something like
c = (a./(b.')).^2;
but you should recheck the sizes.
  댓글 수: 1
Ali Najem
Ali Najem 2022년 11월 8일
Thank you so much for prompt answer.
I think "Index in position 1 is invalid. Array indices must be positive integers or logical values" in
c(j,:) = (a./b(i)).^2;
and also how can use i loop for eliminate using automatic expansion?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by