How to put unknown no. of elements array in one array from a loop?

Hi I want to store the all the factors calculated in the loop in one array(horz)
m a beginer....tried cell ..cat..etc..but since the no.of factors are unknown ...it keeps displaying an error...can smone help...
Thanx Babita
clc;
p=input('No. of periodic tasks');
for i=1:p
p_prd(i)=input('period of periodic task')
end
.......
x=cell(p:1)
for m=1:p
infac=factor(p_prd(m))
x{m}=infac{1}
..........
%fact=ones(1,100)
% for h=1:p
% infac=factor(p_prd(h))
% fact(infac)=cat(infac)
% % z(100)= horzcat(fac(100))
% end
................... all i want is to get the factors of p_prd into one horizintal array for further processing....

3 个评论

Ok, so in a nutshell. You have a matrix p_prd, and all you need is to turn that into one horizontal array?
Is that correct?
no i want factors of p_prd in one array
One vector or a cell array of various-length vectors?
I showed how to get either...

请先登录,再进行评论。

 采纳的回答

factorsarray=cell2mat(arrayfun(@factor,p_prd,'uniformoutput',false));
returns an array of all factors in one horizontal vector. If need to know from which of the various array elements each group of factors comes from, remove the cell2mat wrapper and will get a cell array of length(p_prd) each cell of which contains the respective factors.

3 个评论

clc; p=input('No. of priodic tasks'); p_prd=0
for i=1:p p_prd(i)=input('period of periodic task') end
for m=1:p
infac=factor(p_prd(m))
fff=arrayfun(@factor,p_prd,'uniformoutput',false)
end
.....did not work :(
For what definition of "did not work"?
For one reason at least, you placed it in the wrong place -- get rid of the second loop; that's what arrayfun is for, to operate on the elements of the array.
After collecting the inputs in the first loop, all you need is the line
fff=arrayfun(@factor,p_prd,'uniformoutput',false);
if you want the cell array. No m loop needed (or wanted, or allowed)...
From the command line with a presumed input array of [3 22] for p_prd, I get
>> arrayfun(@factor,[3 22],'uniformoutput',false)
ans =
[3] [1x2 double]
>> ans{2}
ans =
2 11
>> cell2mat(arrayfun(@factor,[3 22],'uniformoutput',false))
ans =
3 2 11
>>
The last is all in a vector per the literal of your first request. >

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by