Add index matrix to matrix

3 ビュー (過去 30 日間)
ha ha
ha ha 2017 年 8 月 31 日
編集済み: ha ha 2017 年 9 月 2 日
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 Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!