array name indexing in matlab
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi
I have a 5x1 cell called C in matlab. Each of its columns contains a 1x20 double array. I want to do the following now:
I want to create 10 new cell arrays (c_1 to c_10), each of the format 1x5, again with each of the 5 columns containing a 20x1 double array. However now, the newly created cell should contain in its double arrays only zeros, except for two lines which are the same as what the array C contained. So for instance the new cell c_1 would have in the two first lines of the 5 double arrays just what C had in its two first lines of its double arrays, but all the rest would be zero. c_2 then has in its third and fourth line what the double arrays from C had in their third and fourth line, but all the rest is zero etc.
Ideally, I would like to loop and create 10 different c_j, but I have no idea how I can do that in Matlab. Here some Matlab-like pseudo code:
for j = 1:19
for i = 1:5
c_j = zeros(20,5);
c_j(j:j+1,i) = C{1,i}(1:2,:)
j=j+2;
end
end
many thanks for any help.
3 Kommentare
Antworten (3)
Guillaume
am 13 Jun. 2016
As Azzi said, created individual arrays is not a good idea. Do read the link that he posted. Individual variables can't be looped over, which is exactly what you want to do.
As a rule, if you're numbering variables you're doing it wrong. The way to do it is to actually store your c_1 ... c_10 into another cell array: c{1} ... c{10} (with a better name).
Since all your vectors are the same size, there's actually no reason to use cell arrays anyway. You may as well store them all in a matrix which makes manipulation easier:
%C: a cell array containing row vectors of the same size
C_matrix = vertcat(C{:}); %convert to matrix
%replicate C_matrix
num_pages = size(C_matrix, 2) / 2;
num_cells = size(C_matrix, 1);
newC_matrix = repmat(C_matrix, [1, 1, num_pages]);
%build a filter any way you want. This works:
filter = reshape(permute(repmat(eye(num_pages), 1, 1, num_cells*2), [3 2 1]), num_cells, num_pages*2, []);
%multiply by filter to keep only what you want:
newC_matrix = newC_matrix .* filter
%if you really want 10 cell arrays:
newC = squeeze(num2cell(newC_matrix, [1 2]));
% if you really want each cell arrays to contain vectors:
newC = cellfun(@(c) num2cell(c, 2), newC, 'UniformOutput', false);
3 Kommentare
Stephen23
am 16 Jun. 2016
Bearbeitet: Stephen23
am 16 Jun. 2016
"I really dont know why I am being corrected on that point"
It is okay that you don't know this, because you can learn why! Because these friendly MATLAB experts have given you some advice: looping over variable names is slow, buggy, and a bad way to write code. Do you want to be given good advice ("Don't loop over variable names") or be given bad advice ("do whatever you want to do") ?
You have a choice: read the wiki page, learn why looping over variable names is slow, buggy, and a really bad way to code. Then you will also learn why these people are giving you this advice. This is called learning: sometimes people do this, because learning better ways to do things makes their work easier, better, faster, and more reliable.
It is very unlikely that you would actually need lots of separate variables: if you just want to loop over them, then creating variable names would be the worst choice of coding. This does not change just because you want to do this. This is independent of your opinion or what you believe you should do: looping over variable names will be slow, complicated, and hard to debug. You can want anything, but that is the reality.
Guillaume's advice to use one single array is likely the best solution.
Guillaume
am 16 Jun. 2016
"Well I asked for cell array because I will need the data for another program which only can use cell arrays"
At the end of my answer, I showed you how to go back to cell arrays, even though I think it's a complete waste of time.
Azzi Abdelmalek
am 13 Jun. 2016
This is not recommended http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
4 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!