Add index matrix to matrix

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}*

9 Comments

KL
KL on 31 Aug 2017
Edited: KL on 31 Aug 2017
What about your other post?
You're going to ignore all the suggestions and advices and just going to ask another question?
ha ha
ha ha on 31 Aug 2017
Edited: ha ha on 31 Aug 2017
Sorry. I think my old post is not clear. After I read many advices, I think my question is not good and not clear.Someone misunderstood the question
Stephen23
Stephen23 on 31 Aug 2017
Do not try to create or access variable names inside the while loop. You should read this to know why, and what the better alternatives are (hint: indexing):
ha ha
ha ha on 1 Sep 2017
Do you have any suggestion for my question? Thanks
This reads like a homework question.
ha ha
ha ha on 1 Sep 2017
Hi Walter Roberson,
It doesn't matter "homework,..." or whatever. If you can help me to find the answer, you're appreciate alot. THANKS
It is difficult for me to make suggestions when you do not answer my questions.
KL
KL on 1 Sep 2017
Edited: KL on 1 Sep 2017
It's not bad if you say it's a homework. Just so that we could help you accordingly. Some of the homework questions could be solved with some built-in Matlab functions without having to write multiple lines of code. Mind you, we are only here to help.
ha ha
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.

Sign in to comment.

 Accepted Answer

B = [];
ii = 1;
x = [9 6 4 11];
add1 = 0;
while ii <= 4
add1 = add1(end) + (1:x(ii));
B = [B;{add1}];
ii = ii + 1;
end
A = [[B{:}]',repelem((1:numel(B))',cellfun(@numel,B))];

5 Comments

ha ha
ha ha on 1 Sep 2017
Yes, correct. Thanks so much Andrei Bobrov.
Stephen23
Stephen23 on 1 Sep 2017
Edited: Stephen23 on 2 Sep 2017
@ha ha: why do you want to use such a complex solution? My answer is much simpler and more efficient (no repelem, no slow cellfun, no cell array, no conversion from cell array to numeric).
ha ha
ha ha on 1 Sep 2017
Sorry @Stephen Cobeldick.
I just see your answer few mins ago. Very sorry. Let's me check carefully your code again, and will inform you later.
No, this requires changing your code, which you have indicated is not permitted.
ha ha
ha ha on 2 Sep 2017
@Stephen Cobeldick and @ Andrei Bobrov
Both your code is GOOD.
BIG THANKS

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 1 Sep 2017
Edited: Stephen23 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 Comments

+1
@ha ha:
A = NaN(0,2);
itr = 1;
while ...
B = ... your code here
B = B(:);
B(:,2) = itr;
A = [A;B];
itr = itr + 1;
end
ha ha
ha ha on 2 Sep 2017
Thank @Stephen Cobeldick,
Your code is perfect.

Sign in to comment.

Walter Roberson
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) ?

6 Comments

ha ha
ha ha on 1 Sep 2017
Edited: ha ha on 1 Sep 2017
Sorry, I mistype. Please see the "edit question" ( All B1,B2,B3,B4..., index_B1,index_B2... : is array)
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) ?
Once the vector B has been built, there is no way to find out which parts were added by which loop. If you do not have information about how long each section is, and you do not have some way of recognizing a pattern in the data marking the beginning of a block, then there is no way to figure out afterwards which data belongs to which loop index.
ha ha
ha ha on 1 Sep 2017
Is the length of each subsection somehow known?
Answer: NO
But, i think my question is possible to find the solution. Why?
Because, if you run (While...end) loop, you will get the array result Bi. For each loop, we just get that array Bi. And then create a new cell including all array Bi for all loop
You said "Remember: you can not see the result array Bi at each loop" and "we just get the FINAL array B for all loops".
If this were homework, then this would be understood as a condition being imposed by outside for learning purposes, that the point of the question is to figure out how to separate the parts of the array given only the final version of it.
If this were not homework, then the obvious answer would be to change the existing code to generate the counts as we go.
ha ha
ha ha on 1 Sep 2017
Dear Walter Roberson,
Thank you so much for your help.
Actually, my algorithm inside the (while...end) loop is very long and complicated. In this post, I try to simplify it too easy to understand what I need help from you alls. But in this code, I need to use (while...end) loop to find each array Bi at each loop. I can not change "the existing code".
Last but not least, the (while...lop) MUST be used to generate the output array B
Please help me.
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?

Sign in to comment.

Categories

Tags

No tags entered yet.

Asked:

on 31 Aug 2017

Edited:

on 2 Sep 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!