Info
此问题已关闭。 请重新打开它进行编辑或回答。
What does this mean? "In an assignment A(I) = B, the number of elements in B and I must be the same".I am having problems with executing this code.
1 次查看(过去 30 天)
显示 更早的评论
for t = 0:100:86164.09164
for i = 0:100:86164.09164
nu = wE*t;
rECI(i) = [rE*cosd(t2)*cosd(nu);rE*cosd(t2)*sind(nu)*cosd(i)+rE*sind(t2)*sin(i);-rE*cosd(t2)*sind(nu)*sind(i)+rE*sind(t2)*cosd(i)];
end
end
2 个评论
Adam
2015-3-5
Please format code in a '{} Code' block.
It's hard to explain that error without repeating what it says! When you are assigning something with an '=' operation you have to have the same number of elements on the right hand side of the assignment as on the left unless the right-hand side is a scalar.
If you end up with e.g. a 4x1 vector on the right-hand side of an expression andd you try to assign it to something that is of size 1x4 then you will get an error like that.
回答(2 个)
Stephen23
2015-3-5
编辑:Stephen23
2015-3-5
There are multiple issue with this code. Lets have a look at some of them:
- indexing in MATLAB starts at one, so the very first iteration of the inner-loop will cause an error as it has a value of zero, and is used to index into this variable: rECI(i).
- Even if it worked without error, the indexing of this variable rECI(i) also would produce a relatively sparse matrix: the first value, then next inserted at position 100, thereafter at 200, and so on. MATLAB fills in the values in between with zeros, so you will get something like this:
rECI = [val1,0,0,0,0,0,0,0,0,..... 0,0,val2,0,0,0,0,0.... etc]
- The code expands the size of rECI on every iteration, which is slow and inefficient. For more efficiency you can preallocate the matrix to the final size before the loop.
- The allocation of a vector with three elements into a single element causes the error message. In MATLAB it every element has its own location, and the allocation that you are attempting is makes no sense:
A(1) = [8,9] % error: multiple elements into one position.
A(1) = 1 % not an error: one element, one position.
A(1:2) = [8,9] % not an error, two elements, two positions.
However this calculation would be most improved by being vectorized. This would simplify your code significantly, be neater and faster. For example the first of your three calculations would look like this:
t = 0:100:86164.09164;
vec = rE*cosd(t2)*cosd(wE*t);
0 个评论
Image Analyst
2015-3-5
Your line
rECI(i) = [rE*cosd(t2)*cosd(nu);rE*cosd(t2)*sind(nu)*cosd(i)+rE*sind(t2)*sin(i);-rE*cosd(t2)*sind(nu)*sind(i)+rE*sind(t2)*cosd(i)];
is essentially like this
rECI(i) = [a; b; c];
but rECI(i) is just one element. You're trying to stuff 3 numbers into an element that can take only 1 number. What do you expect the i'th element of rECI to contain? One number or three? It can take only 1. If you need/want 3 then you'd have to make rECI into a cell array.
4 个评论
Image Analyst
2015-3-6
t and i both have 862 possible values that they take on. So, if eECI is an 862 row by 3 column matrix, you can put this before your for loops:
rECI = zeros(862, 3);
Then in the loop if you want to assign 3 elements to row "i" of rECI, then do this:
rECI(i, :) = [value1, value2, value3];
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!