Issues updating vector inside a for loop

clc, clear, close all;
x3 = 0.5*200; % Half the domain width
x = load("t_10s.txt");
L = x(:,1); % Length/coordinates
[l, m] = size(x);
r1 = zeros(m-1,1);
for i = 1:m-1
n = x(:,i+1); % value of n at the corresponding (L) location
% Find values of L for which n = 1
ind1 = find(n>=0.999);
x1 = L(ind1,1);
r = max(x1) - min(x1); % 2*radius?
r1(i,1) = r
end
% r = max(x1) - min(x1)
% r1(i) = r
Hi all, I am having an issue with updating my vector output from this code. I want to store all the calculated r values from each iteration into a column vector r1 of size (m-1,1), however, I keep getting a size incompatibility errors at line r1(i,1)=r. Could someone help out, please. Thank you.
By the way, I load x (attached) in as a matrix the first column of which I assign to vector L, and columns 2 through m are the different n-vectors for each iteration. Thank you!

 采纳的回答

When find doesn't find any values > 0.999, it returns an empty vector, with a size of 0x1. Furthermore, max returns an empty vector even if the other value is a number. 0x1 empty vectors don't fit in a 1x1 slot like r(i,1).
The error text is often helpful: Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 0-by-1.
r1(i,1) = r
left side (1x1) right side (0x1)
You could test for the empty value and, for instance, convert it to a NaN:
if isempty(r)
r(i,1) = nan;
else
r(i,1) = r;
end
Incidentally, for your future reference, r(i) is equivalent to r(i,1) in this case (and L(i) to L(i,1)). Since the second dimension of r (and L) has size 1, the value only has one column in which it is possible to be placed.

5 个评论

Thanks Chris, for your response. I have tried that and I am still getting r1 output only zero entries like the preallocation. Perhaps, is there a better way to find the indexes of elements in n that are >=0.999, and use those indexes to find their corresponding entries in L? Thank you.
Sorry...I left out the "1".
I'm not sure whether this code accomplishes what you want, though.
x3 = 0.5*200; % Half the domain width in the neck growth direction
x = load("t_10s.txt");
L = x(:,1); % Length/coordinates along of line through neck
[l, m] = size(x);
r1 = zeros(m-1,1);
for i = 1:m-1
n = x(:,i+1); % value of order parameter at the corresponding (L) location
% Find values of L for which n = 1
ind1 = find(n>=0.999);
x1 = L(ind1,1);
r = max(x1) - min(x1); % Neck radius?
if isempty(r)
r1(i,1) = nan;
else
r1(i,1) = r;
end
end
figure
plot(r1)
xlim([0 inf])
Hi Chris, I really appreciate the effort, unfortunately, it is still not accomplishing the goal. With the attached text file, r1 is uspposed to be a 21-by-1 vector with a few first zero entries and the remaining are positive real numbers. However, when I run your modified code, I get the entries of r1 to be almost all NaNs. Thanks once again.
Hi Buhari,
Max values >.999 don't occur until i==13, so you only get nine out of 21 non-zero points, based on your code.
Hi Chris,
Thank you for your input, I really appreciate the help.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File 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