# Add index matrix to matrix

43 views (last 30 days)
ha ha on 31 Aug 2017
Edited: ha ha on 2 Sep 2017
I have some algorithm including a while loop::
while (condition)
.......................................................
.......................................................
.......% do something and return a result array B...
end
Example: I MUST run the code (while...end). For instance, my code will loop 4 time to satify the condition. Therefore, I will get the result of each loop is {B1,B2,B3,B4}
Let's say:
-Loop 1: B1=[1 2 3 4 5....9];
-Loop 2: B2=[10 11 12....15];
-Loop 3: B3=[16 17 18 19] ;
-Loop 4: B4=[20 21 22....30];
The question is: from the result matrix Bi (at each loop):
1/ How to create array index_Bi according to the number of element in array Bi as follows:?
index_B1= [1 1 1 .....1] {9 element 1}
index_B2= [2 2 2.....2] {6 element 2}
index_B3= [3 3 3 3] {4 element 3}
index_B4= [4 4 4.....4] {11 element 4}
_ Note: the number of array "index_Bi" depend on the number of loop (i). In this example, Assumed that the number of loop i=4._
2/ After that, How to add two array (Bi & index_Bi) to matrix A, we will have the result:
A=[ B1 index_B1
B2 index_B2
B3 index_B3
B4 index_B4]
I hope the result of matrix A will be as follows:
A=[ 1 1
2 1
3 1
...1
9 1
10 2
11 2
....2
15 2
16 3
17 3
18 3
19 3
20 4
21 4
.....
30 4]
In the above example, I assume the algorithm is just loop 4 time (i=4). { But actually, For my real data, "the while loop" may be loop up to 100 times (or more). Therefore, i may be equal or larger than 100}*
ha ha on 1 Sep 2017
Dear KL, Thank you so much for your help. But in this code, I need to use (while...end) loop to find each array Bi at each loop.
Actually, my algorithm is very long and complicated. In this post, I try to simplify it too easy to understand what I need help from you alls. Please help me.

Andrei Bobrov on 1 Sep 2017
B = [];
ii = 1;
x = [9 6 4 11];
while ii <= 4
ii = ii + 1;
end
A = [[B{:}]',repelem((1:numel(B))',cellfun(@numel,B))];
ha ha on 2 Sep 2017
@Stephen Cobeldick and @ Andrei Bobrov
BIG THANKS

Stephen Cobeldick on 1 Sep 2017
Edited: Stephen Cobeldick on 1 Sep 2017
You can do this very simply inside the loop, there is no need to use cell arrays, slow cellfun, or repelem.
out = NaN(0,2);
itr = 1;
while ...
tmp = ... your code here
tmp = tmp(:);
tmp(:,2) = itr;
out = [out;tmp];
itr = itr + 1;
end
##### 2 CommentsShowHide 1 older comment
ha ha on 2 Sep 2017
Thank @Stephen Cobeldick,

Walter Roberson on 1 Sep 2017
Edited: Walter Roberson on 1 Sep 2017
index_B4= [4 4 4;.....4] {11 element 4}
The above is not possible. The space between the first two "4" tells us that you have multiple columns, and the ';' after the third "4" tells us that you have multiple rows. In MATLAB, each row must be the same size. Therefore, the total number of elements must be divisible by the number of rows. But the total number of elements is 11, which is a prime number; to have that work out, you would need to have only one column per row -- but we see that there a multiple columns per row, so the total number of elements cannot be prime. If there are multiple rows and multiple columns in an array, then the total number of elements must be a composite number, not a prime.
The creating of 11 copies of element 4 is easy:
repmat(4, 1, 11)
However, this requires that you somehow know that the proper length is 11.
Is the length of each subsection somehow known? Is there something particular about the first element of each subsection (for example if the first element were always negative but the others were always positive) ?
Walter Roberson on 1 Sep 2017
Your restrictions make it impossible to solve the problem.
Suppose, for example, that I told you that the following list of numbers was generated in bursts, and I asked you to identify each burst:
1 5 10 7 9 2 4 3 3 6 8 7 8 7 7 3 1
I can offer the additional information that no burst was negative length, and that no burst had length more than 16383. What were the burst boundaries?