Combine output from for loop into one long vecor, then put that row into a cell.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
Star Strider
on 20 Aug 2015
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
Ellie
on 20 Aug 2015
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?
Ellie
on 20 Aug 2015
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
Star Strider
on 20 Aug 2015
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?
Ellie
on 20 Aug 2015
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]);
Ellie
on 20 Aug 2015
I already have the frequencies. Now I need to make the corresponding number appear that many times. I explained it above.
Star Strider
on 20 Aug 2015
i don’t understand. If you already have the frequencies, then you already have the vectors you want.
Ellie
on 20 Aug 2015
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.
Ellie
on 20 Aug 2015
Fantastic! Thank you so much!!
Star Strider
on 20 Aug 2015
My pleasure!
I’m very happy we got this sorted!
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)