can meaning of ~= be "equal to" ?
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Respuestas (3)
dpb
el 17 de Jul. de 2020
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.
Image Analyst
el 17 de Jul. de 2020
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
Walter Roberson
el 17 de Jul. de 2020
Editada: Walter Roberson
el 17 de Jul. de 2020
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.
0 comentarios
Ver también
Categorías
Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!