How to write such a loop?
Show older comments
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 Comments
Image Analyst
on 18 Jan 2015
If IDX1 is a list of the class indexes that the elements of d1 belong to, then IDX1 should have the same number of elements. So IDX1(1) says that d1(1) belongs to class 1, and IDX1(2) says that d1(2) belongs to class 2, and IDX1(3) says that d1(3) belongs to class 1, etc. So why does your IDX1 have only 5 elements instead of 10? Which elements of d1 have their classes specified by IDX1, and which do not and are missing?
Next you say "Number of members in each segment are segMem1 for d1...." but we can see that segMem1(1) is 3 but we can see from IDX1(1) that the first class shows up two timed, not 3. Just look at IDX1 - there are 2 ones in it, not 3 meaning that 2 elements of d1 were labeled as being in class 1.
So I'm confused by your explanation and need more clarification.
dpb
on 18 Jan 2015
Ayup...same things I thought (but was too lazy to enumerate :) )...
Negar
on 18 Jan 2015
Negar
on 18 Jan 2015
Negar
on 18 Jan 2015
dpb
on 18 Jan 2015
Wrong answer!!! Don't expect us to try to debug non-working code; gives us the actual algorithm to try to implement.
Negar
on 18 Jan 2015
dpb
on 18 Jan 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...
Negar
on 18 Jan 2015
Accepted Answer
More Answers (1)
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!