Add index matrix to matrix

2 次查看(过去 30 天)
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 个评论
KL
KL 2017-9-1
编辑:KL 2017-9-1
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 2017-9-1
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
Andrei Bobrov 2017-9-1
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 个评论
Walter Roberson
Walter Roberson 2017-9-1
No, this requires changing your code, which you have indicated is not permitted.
ha ha
ha ha 2017-9-2
@Stephen Cobeldick and @ Andrei Bobrov
Both your code is GOOD.
BIG THANKS

请先登录,再进行评论。

更多回答(2 个)

Stephen23
Stephen23 2017-9-1
编辑:Stephen23 2017-9-1
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 个评论
Andrei Bobrov
Andrei Bobrov 2017-9-1
编辑:Andrei Bobrov 2017-9-1
+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 2017-9-2
Thank @Stephen Cobeldick,
Your code is perfect.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-9-1
编辑:Walter Roberson 2017-9-1
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 个评论
ha ha
ha ha 2017-9-1
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.
Walter Roberson
Walter Roberson 2017-9-1
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!