i want to get vectors of same length in my result.

2 次查看(过去 30 天)
Hello, need help in the following problem. BC is 1x99 and a(i), b(i) and c(i) are 1x30. and i want to plot BC vs a(i), but i am getting following error: In an assignment A(:) = B, the number of elements in A and B must be the same. any help much appreciated
data = load(FF);
mass = data(:,1); %extracting all rows of column 1
deq = data(:,16); %extracting all rows of column 16
BC = (15:10:995);
for i = 1:length(BC)-1;
index = deq>=BC(i)-5 & deq<BC(i)+5;
masstemp = mass(index);
a(i) = min(masstemp);
b(i) = median(masstemp);
c(i) = max(masstemp);
end
  8 个评论
Stephen23
Stephen23 2018-5-16
编辑:Stephen23 2018-5-16
"i dont know why the format of my program changes when i write it here!!!!!"
Because this website uses a very basic form of markup language to allow text to be formatted. You can learn more by reading the help at the top of this page, or with this tutorial:

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2018-5-16
编辑:Jan 2018-5-16
This line is a problem:
N = size(BC,99);
This replies the length of 99th dimension of the array BC. I'm sure, that you do not have a 99 dimensional array. But Matlab accepts this command, because there is the implicit assumption, that all trailing dimensions are be 1.
X = rand(2,3);
size(X, 3)
>> 1
In you code N becomes 1 and the loop:
for i = 1:N-1
is not entered at all, because it runs from 1 to 0. In consequence the variables a,b,c are not created. I guess, that the shown code is a script and that there are some values of former runs in the workspace.
  1. Convert your code to a function to keep the workspace clean. Simply start the text with "function YourFcn".
  2. Fix the size(BC, 99) command. I cannot imagine what is wanted here.
  3. These commands look weird:
a(i,:) = min(masstemp,:);
b(i,:) = median(masstemp,:);
c(i,:) = max(masstemp,:)
This would be meaningful, if min is a matrix, then masstemp is an index and : means, that the complete rows are selected. The code in the original question looks better:
a(i) = min(masstemp);
b(i) = median(masstemp);
c(i) = max(masstemp);
Here the minimum, median and maximum values of the vector masstemp are determined.
Finally, the error message is
Error using scatter (line 62) X and Y must be vectors of the same length.
Your N is a scalar, and a,b,c have N-1 elements. This cannot work. What do you want to draw with these scatter commands?

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by