I want to apply what the norm function does, without using norm.

4 ビュー (過去 30 日間)
ACme
ACme 2017 年 4 月 14 日
編集済み: John D'Errico 2017 年 4 月 14 日
I am trying to write a code with some overlapping circles. I have written a code that works using the norm function, however, I want as little 'higher-level' functions as possible. Is there a way to do this?
I have the following piece of code that runs:
if norm(center(n,:)-[row col])<= radius
circle_pattern(col,row,n) = 1;
end
And when I replace it with this code (I tried to do some research on norm), my code will not plot the same image.
if ((center(n,:)-row).^2 + (center(n,:)-col).^2) <= radius
circle_pattern(col,row,n) = 1;
end
Any ideas on how to fix this?

採用された回答

John D'Errico
John D'Errico 2017 年 4 月 14 日
編集済み: John D'Errico 2017 年 4 月 14 日
Since this seems to be homework, if you don't want to use canned code, I will ask you to think on your own a bit.
At the same time, for whatever reason you have chosen to not use norm here, NOT using canned code written by experts in the field is a BAD idea. You may think that you are being self sufficient. That is NOT a good idea here. Your hand written code will be often less than optimal. It will be less efficient, but often it may have subtle bugs in it that you won't even know exist. WHEREVER possible, use all the tools you have given to you. If you choose to let pride get in your way, your code will be the poorer for it.
In fact, even something as simple as norm had something in there as I recall to fix some problem that can arise, though I seem to recall it having to do with de-normalized numbers.
Anyway, a hint: A norm will have a square root in there, no? So the 2-norm will be the sqrt of the sum of squares.
Or, you can compare what you did compute to radius^2.
  3 件のコメント
John D'Errico
John D'Errico 2017 年 4 月 14 日
編集済み: John D'Errico 2017 年 4 月 14 日
I must be asleep yet. I forgot to add a norm is the sqrt of the sum of squares. You subtract off row, then square the elements, but you need to sum them too.
x = rand(1,5);
sqrt(sum(x.^2))
ans =
0.87087
norm(x)
ans =
0.87087
So what you need to do is this test:
if sum((center(n,:)-[row,col]).^2)) <= radius^2
which will compute the square of the norm, then compare it to radius squared.
Or, if you really don't want to use sum (which is silly not to use) you could have done it as:
if ((center(n,1)-row).^2 + (center(n,2)-col).^2) <= radius^2
At some point, you need to use the basic tools provided. Otherwise, you arguably need to also write your own code for plus, minus, ^2, subscript indexing, etc. Remember that a+b is simply a call to the function as plus(a,b).
Yes, you can do all of that. Having written all of it myself for toolsets like HPF, VPI, VPIJ, SYMPOLY etc., I can say that it will take a great deal of work to do it all well.
Honestly, I think you will find a better use of your time learning how to write vectorized code. It looks like here, you have a simple loop with a test inside, counting the number of times an event occurs. Instead, you might be able to write the entire loop as one line of code.
ACme
ACme 2017 年 4 月 14 日
Perfect!! That is exactly what I needed! My mistake was taking the nth row of all columns instead of taking the nth row of columns 1 and 2 in the 'center' array. (Looking back on it now, it logically doesn't even make sense!) Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by