Combine output from for loop into one long vecor, then put that row into a cell.

Hi all,
I have this code, and it works how I would like for the first row, but I am having trouble generalizing it.
y = randi(10,10);
out = [];
m = sparse(y);
for j1 = 1:N
y1 = (j1-1) * ones(1, m(1,j1));
out = [out y1];
end
Thanks in advance.

 Accepted Answer

Not certain I understand what you want, but if you want ‘out’ to be a cell array, just address it that way:
out{j1} = [out y1];

12 Comments

I think that is along the lines of what I want to do, but not quite there.
If for example, the first row of y consists of [2 3 2], the code will then put this into a vector[0 0 1 1 1 2 2]. The code currently does this fine, but I am wanting to make it more general, to do this for rows 1:N. The problem that I am facing is that each row will be different lengths, so they need to go into a cell.
They are each different length cells. (I had to supply ‘N’.) This produces a (1x10) cell of varying-length double vectors for ‘out’:
y = randi(10,10);
N = size(y,2);
out = [];
m = sparse(y);
for j1 = 1:N
y1 = (j1-1) * ones(1, m(1,j1));
out{j1} = [out y1];
end
What about that doesn’t work in your application?
I am not sure what about it needs to be modified, but it doesn't do what I want. The code needs to be modified in someway, although I don't know how. I have tried this, but it isn't working.
y = randi(10,10);
N = size(y,2);
out = [];
m = sparse(y);
for j1 = 1:N
for j2 = 1:N
y1 = (j1-1) * ones(1, m(j2,j1));
out{j1} = [out y1];
end
end
Well, that certainly clears things up!
What project are you working on, what do you want to do, and how do you want to do it?
Well, for each row, I have a matrix which identifies the number of times an element occurs (starting with 0). For example [2 3 1] would translate to [0 0 1 1 1 2]. I need to do this for each row and I have been having trouble, because they are all different lengths.
I would use the histc (or histcounts) function to get the frequencies. It operates column-wise, and all the vectors are the same lengths. It would seem to me to be easier:
y = randi([0 10], 10);
hy = histc(y, [0:10]);
I already have the frequencies. Now I need to make the corresponding number appear that many times. I explained it above.
i don’t understand. If you already have the frequencies, then you already have the vectors you want.
So, I have the frequency vector [3 2 3 2]. I need to make this into [0 0 0 1 1 2 2 2 3 3].
I get what you're saying, but my code gives the frequency, and I need to have a vector like described.
This seems to work:
fv = [3 2 3 2]; % Frequency Vector Matrix
for k1 = 1:size(fv,1) % Iterate Over Rows Of ‘fv’
V = []; % Define ‘V’
for k2 = 1:size(fv,2) % Iterate Over Each Column Of The ‘fv’ Row
V = [V (k2-1)*ones(1,fv(k1,k2))]; % Concatenate ‘V’
end
out{k1} = V; % Assign ‘out’
end
celldisp(out) % Display Output (Optional)
See if it does what you want.
My pleasure!
I’m very happy we got this sorted!

Sign in to comment.

More Answers (0)

Asked:

on 20 Aug 2015

Commented:

on 20 Aug 2015

Community Treasure Hunt

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

Start Hunting!