Hot to solve an assignment error: assignment A(:) = B during coding
3 views (last 30 days)
Show older comments
Hello, with this code i get an assignment error:
n=1;
for k = phT3
while k < -180
k = k + 360;
end
phT3(n)= k;
n = n +1;
end
In an assignment A(:) = B, the number of elements in A and B must be the same.
phT3 is a vector which each member is the phase of a transfer function at a given frequency (the frequancy is its index = n). What i try to do is to plot it using logplotx unwrapped, so the plot have to be between 180º and -180º. If i try to do phT3(2245)=26, for example, in the regular command line i have no problem, but with the code i get the assignment error, any idea?? Thx! In the exemple phT3(2245) would be the phase in degrees at 2245 rad/s and change it to be 26º.
0 Comments
Answers (2)
dpb
on 12 Dec 2016
Edited: dpb
on 13 Dec 2016
Your error is somewhere not shown; the above code will work as is...not very "Matlab-y" implementation but doesn't cause the error above from only that and the description.
>> phT3=[-200:10:-160]; % sample data
>> n=1;
>> for k = phT3
while k<-180,k=k+360;end
phT3(n)=k;n=n+1;end
>> phT3
phT3 =
Columns 1 through 5
160 170 -180 -170 -160
>>
However, the Matlab way would be more like
ix=(phT3<-180); % logical vector of condition
phT3(ix)=phT3(ix)+180; % operate on those elements
ADDENDUM For brevity I'll note here the issue with the assignment is that your vector is a column vector and for returns the elements column-by-column for the array iterator form; hence the assignment is a "many to one" attempt in trying to assign a vector to a single array element, n. Comments show how works, and all, but to save others from having to read whole thread to get the gist...
2 Comments
dpb
on 13 Dec 2016
It's the orientation of the vector and the behavior of for when iterating over the collection of elements. If, as in my example, the vector is a row vector, for returns each element in turn as your code is expecting. OTOH, if it is a column vector, since the definition is that for each column in the valArray that column is passed as the value and each succeeding column in subsequent passes.
For a short demo--
>> valArray=0:4; % row vector of small integers
n=0; % initialize counter
for k=valArray, % iterate over array
disp([n k]), % see what are n, k each pass...
n=n+1; % increment counter
end
0 0
1 1
2 2
3 3
4 4
>>
Works as my illustration of your original code...now let's repeat except make the array a column vector instead--
>> valArray=0:4;n=0; % same initialization
for k=valArray.', % we transposed the array though to column
disp([n k]), n=n+1; end % try same thing as above for rest
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
>>
AHA! n is a single value, k is now a column vector so fails. Let's show it one more way that will work...same thing as above except we'll transpose the k back to row so the concatenation inside the disp() call will work--
>> valArray=0:4;n=0; for k=valArray.', disp([n k.']), n=n+1; end
0 0 1 2 3 4
>>
Now see that since the valArray is a single column, the loop only interated once but we got all five values besides n at one time. That's the crux of your assignment above; you have length(phT3) elements in the variable k and when try to assign that to subscript n which a single location in the array, "you can't do that!"
Hopefully that explains syntax issue you had; as noted, in Matlab, "Use the vectors, Luke!"; they're the power in Matlab that let's you avoid indexing.
OBTW: the surefire way to iterate over the elements of a vector irregardless of its orientation is to use the "classic" c-like idiom for for...
for k=1:length(phT3)
if phT3(k)<180 phT3(k)=phT3(k)+180;end
end
But, the logical array, no-loop solution is "the Matlab way".
Image Analyst
on 12 Dec 2016
What is k when it bombs? That kind of error occurs like when you try to assign 10 elements to one element. So if n = 1 and k = a vector of 2 or more elements, you'd get that error. So, what is k?
And the biggest danger in your code is using phT3 to get your values of k, but then you're redefining phT3 within the loop! What if you end up redefining an index of phT3 that k has not yet reached? Which value do you want to use: the original one or the new one. For example what if on iteration 2 you redefine phT3(3). What value of k do you use on iteration 3?
Then, to make matters worse, you're also redefining k inside the loop, which is technically allowed but realize that when the iteration finishes and the next iteration begins, the next k will be what the for line dictates, not 1 plus whatever you redefined k to be.
I'm not really sure what you're after so I don't know yet how to fix the code.
1 Comment
dpb
on 12 Dec 2016
Edited: dpb
on 13 Dec 2016
k is the whole array phT3 when it bombs, IA, because he has a column vector instead of row vector as I used.
While it looks unusual, as shown above it actually works as intended if phT3 is a row vector. The problem Ariel has is that phT3 vector is a column vector in which case instead of iterating over the content of the vector individually you get the full vector every time. This behavior is documented...
I forgot about the orientation in the original response; will note it since OP followed up w/ query.
See Also
Categories
Find more on Analysis and Verification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!