nick_name
nick_name

Reputation: 161

How to fill matrix with variable numbers?

I need to fill matrix r, where r=r(z), and ri is constant. But with this code I only get the first row where r=-0.7:0.7.

z=-1:0.001:0;
ri=0.7;
R=ri-z*(ri-1);
for z=-1:0.001:0;
r=(linspace(-(ri-z*(ri-1)),ri-z*(ri-1),1001))
end

meshgrid also does not work because it gives constant values on the end of the rows

My full matrix need to be in this shape, or transpose of this:

-0.7......  0.7

0.8  ...    0.8
.            .
.            .
.            .
0 .9 ...     0.9
.            .
.            .
.            .
1     ...    1

or

0   ......  0.7
0    ...    0.8
.            .
.            .
.            .
0  ...      0.9
.            .
.            .
.            .
0     ...    1

Upvotes: 0

Views: 214

Answers (1)

JAC
JAC

Reputation: 466

As @Ander Biguri said, the problem is that each pass through the loop sets r and overwrites whatever was there from the previous pass. At the end of the loop you get the last row (not the first). Pre-allocate r

r = zeros(numel(z), 1001);

Then loop as

for k=1:numel(z)
   R=ri-z(k)*(ri-1); 
   r(k,:) = linspace(-R, R, 1001);% <-- Different row each pass
end

Hope this helps,

JAC

Upvotes: 0

Related Questions