Need help with factorizing array of numbers
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 2 月 26 日
編集済み: Azzi Abdelmalek
2013 年 2 月 26 日
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 件のコメント
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.
Azzi Abdelmalek
2013 年 2 月 27 日
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 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!