Quick question on understanding ./ notation

1 view (last 30 days)
Greetings all,
I have the following "snippet" of code:
Zeta_EM31_West = DrillHoleData2(:,3)/3.67;
%Line above outputs a 16x1 double array, I can provide "DrillHoleData2" if needed
Response_values_H= 0:0.01:9;
R_h=sqrt(4*Response_values_H.^2+1)-(2*Response_values_H);
R_h(Response_values_H>=9.1) = 0;
R_h_Case1A=interp1(Response_values_H,R_h,Zeta_EM31_West);
Now I'm a bit confused on something. Say the next line is this: sigma_2_Case1A=EM31CondMidSwathWest/R_h_Case1A;
where "EM31CondMidSwathWest" is another 16x1 array (and I can provide the numbers if needed). Now just using / will provide me with a 16x16 double, which I understand, but in one column I'll receive values, and the rest are zeros (I also understand this).
What I don't understand is if I say: sigma_2_Case1A=EM31CondMidSwathWest./R_h_Case1A;
which will be a 16x1 double, but the numbers will be different than those with using "/". Shouldn't they be the same?
Thanks! -J

Accepted Answer

John D'Errico
John D'Errico on 19 May 2015
Edited: John D'Errico on 19 May 2015
If they should be the same for / and ./ I imagine they would not give us TWO different operators?
./ does an element-wise division. It divides each element by the corresponding element, as long as the two vectors/arrays are the same size and shape. It will fail if they are not, unless one of them was a scalar.
/ does the element-wise division IF the denominator is a scalar. If the two arguments are arrays, or vectors, then linear algebra comes into play.
B/A is equivalent to B*inv(A), WHEN A is a square matrix. If A is not a square matrix, then a solve is done that employs a factorization of the matrix A.
READ THE HELP. Of course, I admit that it might not be obvious to look at these:
help slash
help rdivide
help ldivide
help mrdivide
After all, I might have naively looked for help under divide. I honestly think it would be a good idea if this:
help divide
produced links to the appropriate functions, and explained the basic differences between all of those operators.
  3 Comments
Jesse
Jesse on 19 May 2015
John, thanks for your help. Apologies for asking such a basic question, but stepping away from my code for a couple of hours gave me an "a-ha" moment. Sometimes we all need that!
Stephen23
Stephen23 on 19 May 2015
Although the cyclist has already given this link, it deserves a bit more prominence:

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 19 May 2015
I'm not as confident as you are that you understand the difference between the rdivide (./) and mrdivide (/). In particular, the latter command is for solving systems of linear equations, not for simple division.
I suggest you read those documentation pages carefully.
You can read more generally about array vs. matrix operations here.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!