Saving to a vector

조회 수: 4 (최근 30일)
Harel Harel Shattenstein
Harel Harel Shattenstein 2015년 4월 2일
편집: Stephen23 2015년 4월 3일
function [ b ] = PrimeF( x )
for i=1:x
if mod(x,i)==0
b=i;
end
end
b=isprime(b)*b;
end
I have built a simple function the return the prime factorization of a number. but I can find a way to save them into a vector I called b (that I do not know his size). Thanks

채택된 답변

Stephen23
Stephen23 2015년 4월 2일
편집: Stephen23 2015년 4월 2일
Doing this in a loop and concatenating new values into a vector is very poor use of MATLAB, and will be very slow. It would be much better to learn how to write fully vectorized code, which will be much faster, neater and less buggy. Rather than learning bad coding habits and trying to change them later, you should learn from the very start how to write vectorized code in MATLAB.
Note for example that mod works perfectly for an entire array as an input, so you could perform this calculation all in one go, without any loops. We can also use some basic logical indexing to pick the values that we want:
function v = primefact(x)
y = 1:x;
z = mod(x,y);
z = y(z==0);
v = z(isprime(z));
end
Which we can then test to check that it find all of the prime factors:
>> primefact(7)
ans =
7
>> primefact(10)
ans =
2 5
>> primefact(30)
ans =
2 3 5
>> primefact(42)
ans =
2 3 7
>> primefact(43)
ans =
43
Note that this code excludes one from the output.
  댓글 수: 2
Harel Harel Shattenstein
Harel Harel Shattenstein 2015년 4월 2일
Thanks! How does this action called?
y(z==0)
Stephen23
Stephen23 2015년 4월 3일
편집: Stephen23 2015년 4월 3일
I thought you might ask this, which is why I gave you this link in my answer: logical indexing
The code z==0 creates a logical array, and the code y(z==0) uses this logical array as indices into y.
The code z(isprime(z)) is also logical indexing, using isprime to generate the logical array.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2015년 4월 2일
편집: Adam 2015년 4월 2일
You can replace
b = i;
with
b = [b i];
to create an array. You will get a warning about it resizing in a loop, but in this case it isn't easy to presize it and unless you are doing it on large numbers and the time taken is unacceptable it isn't worth worrying about. Even if the time taken is excessive it may not be due to the resizing of the array anyway.
You will also likely have to change your penultimate line to
b = isprime(b) .* b;
to work for a vector using point-wise multiplication (note the extra '.')
  댓글 수: 3
Michael Haderlein
Michael Haderlein 2015년 4월 2일
While preallocation is not easy in this case (as Adam mentioned), you at least need to define the variable. Just add before starting the loop
b=[];
Adam
Adam 2015년 4월 2일
Ah yes sorry, I forgot that. It's what comes of answering off the top of my head rather than checking it in Matlab first!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by