Need help with factorizing array of numbers

hi there,
say you have an array of numbers x =[94 89 124 120 150] how can i find factors of this array, i mean break each number in the array to its primes. i know the function is factor(x). but it only applies to a scalar value not an array. i tried putting it on a loop
x =[94 89 124 120 150] ;
for i = 1:length(x)
facts_x(i) = factor(x(i))
end
but this i realized gives me an error because the loop runs only in the length of x the facts_x ends up bigger because factor answer is stored as an array. please help. thanks SN

댓글 수: 1

Try storing it in a cell array instead:
facts_x(i) = {factor(x(i))}

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 26일
편집: Azzi Abdelmalek 2013년 2월 26일

0 개 추천

You need to use a cell array to store the result for each number, because they are not the same length
x =[94 89 124 120 150] ;
out=arrayfun(@factor,x,'un',0)
out{:},

댓글 수: 5

I'd prefer the explicit loop:
facts_x = cell(1, lenghth(x));
x =[94 89 124 120 150] ;
for k = 1:length(x)
facts_x{k} = factor(x(k));
end
But the result is equal. The runtime will be comparable also, because the main work happens inside factor.
Please, don't add an answer as a comment , click on comment this answer and add your comment.
Did you notice that your results have different sizes
The first result is [2 47]
The second result is 89
Tell how to put them in one matrix?
Jan
Jan 2013년 2월 27일
@Azzi: Do you meant me with "don't add an asnwer as a comment"? Or did you meant the OP and "don't add a comment as an answer" instead?
I've added my code as comment, because it does exactly the same as your asnwer. Therefore this is nothing new.
Jan, sorry for the confusion, I should have mentioned the name of the OP. I meant: don't add a comment as an answer.

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

추가 답변 (1개)

Ahsan Khan
Ahsan Khan 2013년 2월 27일

0 개 추천

thanks for a quick reply. they both work but In either of those cases mentioned above how do i then store this in a single matrix?. thanx in advance. SN

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by