Assign to certain positions of an entire cell array values from another cell array of the same size
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Valeria
am 24 Aug. 2016
Kommentiert: Valeria
am 25 Aug. 2016
Dear community,
sorry if this question was answered somewhere but I could not find a solution to exactly this problem and I have not found functions in Matlab that do what I need.
I have a quite simple problem assigning to an entire cell array values from another cell array of the same size. However I want to assign them to a certain and same positions of the cells. I know I can do the for-loop but I want to avoid it.
Example:
I have a cell array
a_part{1} = [1; 2; 8];
a_part{2}=[11; 31; 15];
a_part{3} =[2; 4; 8];
and I would like to assign it to another cell array which is of the same size (1x3) but can be empty for example
a_whole = cell(1,3).
However I want to assign these values to certain (same) positions in an entire a_whole, say to rows [1, 3, 5]. I want to get
a_whole{1} = [1;0;2;0;8];
a_whole{2} = [11;0;31;0;15];
a_whole{3} = [2;0;4;0;8]};
I tried the following:
a_whole{:}([1,3,5],:) = a_part;
a_whole(:)([1,3,5],:) = a_part;
[a_whole{:}([1,3,5],:)] = deal(a_part);
I get different error messages but no result. However, this works nicely:
for i=1:3
a_whole{i}([1,3,5],:) = a_part{i};
end
I hope there is a better solution.
Thanks for help and your time!
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 24 Aug. 2016
a_part {1} = [1; 2; 8];
a_part {2} = [11; 31; 15];
a_part {3} = [2; 4; 8];
a = zeros(numel(a_part{1})+2,numel(a_part));
a(1:2:end,:) = [a_part{:}];
a_whole = num2cell(a,1);
Weitere Antworten (1)
Azzi Abdelmalek
am 24 Aug. 2016
a_part{1} = [1; 2; 8];
a_part{2}=[11; 31; 15];
a_part{3} =[2; 4; 8]
a_whole = cell(1,3)
out=cellfun(@(x) [x(1) 0 x(2) 0 x(3)],a_part,'un',0)
3 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!