can meaning of ~= be "equal to" ?

11 次查看(过去 30 天)
Ozan Mirzanli
Ozan Mirzanli 2020-7-17
hey everyone,
i was solving a problem in my teachers book, then i couldn't and i checked my teacher's solution.
the asked thing was "please write a program that assigns the loads to the points as shown in the illustration"
and the solution is:
for i=1:n
if rem(i,2)~=0
q(i)=2;
else
q(i)=-3;
end
end
what i didn't understand is why did he used ~= in the if part. In my opinion (according to the illustration) he should've written "==" because it assigns q=2 in factors of 2 i mean factors of 2 means 0 in rem(i,2). where am i wrong?
thanks in advance!
  1 个评论
Stephen23
Stephen23 2020-7-17
编辑:Stephen23 2020-7-17
"because it assigns q=2 in factors of 2 i mean factors of 2 means 0 in rem(i,2). where am i wrong?"
MATLAB indexing starts at one.
The code is not very well written, it would be simpler and easier without that loop.

请先登录,再进行评论。

回答(3 个)

dpb
dpb 2020-7-17
Since MATLAB array counting is 1-based, have to move up by one from the 0-based counting.
The solution produces the expected result if q(1) --> (0,0,0) of the diagram. What the diagram has to do with anything is anybody's guess from the question, though.
It's also not a very "MATLAB-y" solution -- no need for any arrays or if ... else .. end blocks or even a mod function.
Try to think in vector terms instead...you'll progress much better with MATLAB doing so.
  1 个评论
Ozan Mirzanli
Ozan Mirzanli 2020-7-17
Ummms, would you mind if ask you to explain it a little more clear again? like for course books "physics for dummies" 😅

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-7-17
I think he made a mistake. It's been known to happen. My Professor gave us an errata sheet for his book that we were contantly adding to as the class went on.
allSets = [0,0,0; 1,1,1; 2,2,2; 3,3,3; 4,4,4]
for k = 1 : size(allSets, 1)
% Get this set into q:
thisSetOf3 = allSets(k, :);
% Since all digits are the same we only need to check the first digit.
if rem(thisSetOf3(1), 2) == 0 % Using double =
q(k) = 2; % Assign a load of 2
else
q(k) = -3; % Assign a load of -3
end
end
% Show all q:
q
It shows:
allSets =
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
q =
2 -3 2 -3 2
  1 个评论
Ozan Mirzanli
Ozan Mirzanli 2020-7-17
thx a lot, i totally understand your solution. but i don't think there is a mistake by my professor's solution cuz it works and gives the correct answer when i run it.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-7-17
编辑:Walter Roberson 2020-7-17
what i didn't understand is why did he used ~= in the if part. In my opinion (according to the illustration) he should've written "==" because it assigns q=2 in factors of 2
No, the code presented assigns 2 at odd-numbered array indices, not at factors of 2.
What you are missing is that there is a difference between array indices and what value they are associated with.
You should not read q(4) as necessary being associated with the node (4,4,4) : You should read q(1) as associated with the 1st node, q(2) as being associated with the 2nd node, q(3) being associated with the 3rd node, q(4) being associated with the 4th node.
Consider for example a diagram that had nodes numbered like
2 5/2 10/3 17/4 26/5 37/6 50/7
2 is the 1st node, 5/2 is the 2nd node, 10/3 is the 3rd node, and so on. The pattern is that r(K) is associated with the value K+1/K . Each of the nodes can have an associated value, such as 2 or -3 like you had before.
Thus in MATLAB, the i in q(i) has to do with the relative position in the list of values, not with the node number (which in this example is i+1/i ) . You can move between index and associated number at need.
The relative position is not necessarily even computable ahead of time. For example,
x = sort(rand(1,10));
y = sin(acos(x));
then x(5) would be the 5th x, and the "node number" x(5) would be something you cannot predict in advance, and the associated value would be in y.

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by