How to write such a loop?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello everybody, I have two data vectors: d1 and d2, I have passed them through a segmentation algorithm, and the resulted class indexes are IDX1 and IDX2. Number of members in each segment are segMem1 for d1 and segMem2 for d2. Now I want to assign one index to each component in d1 (and d2), according to segMem1 (and segMem2). Moe specifically, I want to index the 3 first components of d1 as 1 1 1, the two next components as 2 2 , the 2 third components as 1 1 and so on, and at last I want to have 1 1 1 2 2 1 1 3 4 4 for d1. I have written the following code, and it seems to work for indexing d1. But I don't know how to make another loop to generalize this to both d1 and d2?
clear all
close all
clc
% data
d1 = [10 3 44 33 4 16 7 10 22 54];
d2 = [3 24 35 12 3 4 7];
IDX1 = [1 2 1 3 4];
IDX2 = [3 1 2 1];
segMem1 = [3 2 2 1 2]; % Determines how many times each index in IDX1 should be repeated.
segMem2 = [2 1 2 2];
%%Now assign indexes to data vector
n = 1;
a = cell(1,length(d1));
for k = 1:length(segMem1)
for j = n:segMem1(k)+n-1
a{j} = IDX1(k);
end
n = n+segMem1(k);
end
Any help would be greatly appreciated.
9 comentarios
dpb
el 18 de En. de 2015
...I can not make it done for more than one data vector.
Then it doesn't work, does it? If it did, you wouldn't have a problem.
"Houston, we have a problem" in that the algorithm isn't well-enough defined by your initial description from which to write a piece of code. It's quite possible the actual solution is far removed from your first pass and the only practical way to attack it is to know the requirements first, not to try to guess how to hack on a given piece of code without answers to the questions posed by IA...
Respuesta aceptada
dpb
el 18 de En. de 2015
Editada: dpb
el 18 de En. de 2015
>> cell2mat(arrayfun(@(x,y) repmat(x,y,1),IDX1,segMem1,'uniform',0).')
ans =
1
1
1
2
2
1
1
3
4
4
>>
Since Matlab doesn't know about jagged arrays other than by cell array, you'll have to build the pieces and rearrange them into a cell array.
Oh, a spurious thought though I've never tried it; I suppose one could wrap a cellfun call around arrayfun, maybe to process the two cells as you've smushed them together above; otherwise a loop or the cell array.
ADDENDUM
OK, the looping construction works well enough...
>> for i=1:length(IDX)
idx=IDX{i};
seg=s{i};
cell2mat(arrayfun(@(x,y) repmat(x,1,y),idx,seg,'uniform',0))
end
ans =
1 1 1 2 2 1 1 3 4 4
ans =
3 3 1 2 2 1 1
>>
Left as row vectors for more compact display...
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!