Cannot Understand a Command
2 ビュー (過去 30 日間)
古いコメントを表示
I was working for assignment and most of the variables are given already. The Gm is an array with M numbers of row and N numbers of column. The command is to find "E(a,b) = " The "E" is a newly assigned variable and the command is given and I have to write the part after "=" sign. But I cannot understand what is the use of syntax "E(a,b) = " so, I can write my part.
4 件のコメント
dpb
2024 年 12 月 20 日
You're the one who's going to do the assigning by adding something to the right hand side (RHS) after the "=" sign.
MATLAB does allocation on assignment so it isn't mandatory to have previously assigned E; it will be whatever is the result of evaluating the RHS.
Now, when you do subscript a variable, then the sizes of the quantities represented by the subscripts on the LHS and the result of the RHS will have to match.
As @Torsten notes, without the actual context we can't really answer unequivocally for the specific case, but given your later reply, it looks like if there are multiple G, then probably the a,b subscripts here refer to cell arrays when contain the particular parts of G, M. Or, depending upon how the rest of the code was written, it may be iterating over every element of the 2D arrays--we simply don't have sufficient information to know.
Of course, when you convert to Energy from Power, it's going to take two Power values separated by some time interval to compute so you'll end up with one less value in the time direction than starting with.
Steven Lord
2024 年 12 月 20 日
E is a new variable as well as a and b. They never assigned before .
I'm wondering, do you have previous programming experience with a language in which you need to declare your variables before using them (such as C or C++?)
MATLAB doesn't require that. You can effectively declare the variable as you're defining it, by assigning a value to a name that doesn't exist.
whos E % Note that no such variable exists so there's nothing to display
E(1, 2) = 42 % Assign to E, creating it
whos E % Now it exists
Moreover, the matrix G isn't a smiplimple matrix have a value for m rows and n column but also obtained through loop, so it means there are multiple G matrx.
No. The contents (and perhaps even the size and type) of the G matrix may change, but there's only one G matrix at a time in your workspace.
Note that the code below will show whos output 5 times, once per loop iteration, but there's only ever one G matrix in the workspace. The variable G changes size at each loop iteration, but you never have two variables named G existing simultaneously.
for k = 1:5
G = magic(k); % Create/overwrite the variable G with a new matrix
whos % Should show only E (from above), loop variable k, and one G matrix
end
回答 (1 件)
John D'Errico
2024 年 12 月 20 日
I would guess what they did was to ask you to write a matrix by something like the following description:
M(a,b) = a + b
where a and b vary from 1 to 5.
In the above form, a and b are not given in advance, only the range for a and b. You might recognize the matrix is effectively just an addition table.
There are many ways you could accomplish the task. One way would be to use loops. Thus, loop over a and b. That would look something like
M = zeros(5,5); % pre-allocating M for efficiency
for a = 1:5
for b = 1:5
M(a,b) = a + b;
end
end
Do you see how this works, and what was intended?
Of course, there are far better ways to accomplish this task in MATLAB. But typically, when such a desciption of the intended form of a matrix is given as I have shown, this is what they want.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!