Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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 Ansicht (letzte 30 Tage)
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 Kommentare
Adam
Adam am 5 Mär. 2015
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.
saikrishna gadde
saikrishna gadde am 5 Mär. 2015
Hey i know what you are saying but i want to run rECI(i) from i=1:100:86164.09164 and also simultaneously vary "t" which inturn varies "nu".How can i do that,so its like rECI(1) = [rE*cosd(t2)*cosd(1);rE*cosd(t2)*sind(1)*cosd(i)+rE*sind(t2)*sin(i);-rE*cosd(t2)*sind(1)*sind(i)+rE*sind(t2)*cosd(i)];
once see the above formula i am solving for i = 1 and t = 0(which is nu = 0) and i want to repeat that process from 1 to 86164.09164 for both of them by making 100 as my step size.

Antworten (2)

Stephen23
Stephen23 am 5 Mär. 2015
Bearbeitet: Stephen23 am 5 Mär. 2015
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);

Image Analyst
Image Analyst am 5 Mär. 2015
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 Kommentare
saikrishna gadde
saikrishna gadde am 6 Mär. 2015
What do you mean by pre-sizing?I just want to increase "i" with simultaneously increasing "nu".Like as "i" goes from 1 to 86165 simultaneously increasing "t" also from 0 to 86164 with step of 100 for both of them.As "t" varies "nu" also varies.
Image Analyst
Image Analyst am 6 Mär. 2015
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!

Translated by