Hi all,I am trying to do the following: Let's say I have two matrices
A =
2
2
1
3
B =
0.1000
0.0030
0.0500
0.0200
0.4000
0.0600
0.0900
1.0000
Every value from matrix A represents the number of values I would like to extract from matrix B. For example: if the first value in matrix A is 2, then I would like to extract first two values from B in a row [0.1,0.003] and so on.

4 件のコメント

Walter Roberson
Walter Roberson 2017 年 11 月 19 日
The 2 that is the second value of A: is that also to extract the first two elements of B, or is it to extract the next two elements of B ?
Doina Gumeniuc
Doina Gumeniuc 2017 年 11 月 20 日
Hi Walter, first 2 from A means first 2 values from B. Second 2 from A, means the next 2 values from B. So by the time we get to 3 in A, from B we will extract the last 3 values: 0.06, 0.09 and 1. We have 8 values in B, which is 2+2+1+3 in A.
Rik
Rik 2017 年 11 月 20 日
And how did you want to save those different parts?
Doina Gumeniuc
Doina Gumeniuc 2017 年 11 月 21 日
Hi Rik! I will fprint them into a vtk input file. In B I have numerous columns so I would like to take it column by column using the same values from A

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 20 日

1 投票

B_cell = mat2cell(B, A, size(B,2));

5 件のコメント

Doina Gumeniuc
Doina Gumeniuc 2017 年 11 月 21 日
編集済み: Doina Gumeniuc 2017 年 11 月 21 日
Hi Walter. This does the job, but how could I extract it in a matrix instead of cell array? On the display I would like to see directly the numbers separated by space. Thank you very much, it would be exteremly helpful!
Walter Roberson
Walter Roberson 2017 年 11 月 21 日
Doina Gumeniuc
Doina Gumeniuc 2017 年 11 月 26 日
編集済み: Doina Gumeniuc 2017 年 11 月 26 日
Hi Walter, thank you for the link! I have worked it out in the end to get what I need. I have another concern: what if in B I have multiple columns? And I would like to do the same for each. I have first transformed each column in cell with
C=num2cell(B,1);
Then I have tried
B_cell = mat2cell(B{:}, A, size(B{:},2));
But it gives me error :( would it be possible another way? I am trying with cellfun now. I have 50 columns. Thank you very much!
Walter Roberson
Walter Roberson 2017 年 11 月 26 日
B_cell = mat2cell(B, A, ones(1,size(B,2)));
Doina Gumeniuc
Doina Gumeniuc 2017 年 11 月 26 日
wow..Great, thank you a lot!

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2017 年 11 月 19 日

0 投票

A solution that might not scale well, is to convert A to a cell (with num2cell), use cellfun with @(x) 1:x, make sure it is a row array, convert back to a normal vector (with cell2mat) and use that as the index to B (so C=B(index_vector);)

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by