Let's say: A=zeros(7,2):
A=[ 0 0
0 0
0 0
0 0
0 0
0 0
0 0]
B=[4 1 3] ; C=[1 1 1] ;
D=[2 5 7 6]; E=[2 2 2 2]
How can I add two matrix B,C,D,E to matrix A to get the result as follows:
F=[ 4 1 ......add B, C.......
1 1 ......add B, C.......
3 1 ......add B, C.......
2 2 ......add D, E.......
5 2 ......add D, E......
7 2 ......add D, E......
6 2] ......add D, E......
In general, let's say :
A=zeros(1000,2)
B1=[1; 2; 3;....100] ; index_B1= [1;1;1;.....1] {100 element 1}
B2=[101;102;103....300]; index_B2= [2;2;2;.....2] {200 element 2}
B3=[301;302;303....600]; index_B3= [3;3;3;.....3] {300 element 3}
..................................................................
B7=[801;802;803....850]; index_B7= [7;7;7;.....7] {50 element 7}
...................................................................
Bi=[921;922;923....1000]; index_Bi=[i;i;i;.....i] {80 element i}
1/ How to create matrix index_Bi according to the number of element in matrix Bi? (If possible please use while or for ...loop to do)
2/ After that, How to add two matrices (Bi & index_Bi) to matrix A, we will have the result:
F=[ B1 index_B1
B2 index_B2
B3 index_B3
..............
B7 index_B7
.............
Bi index_Bi]

1 commentaire

Stephen23
Stephen23 le 31 Août 2017
Modifié(e) : Stephen23 le 31 Août 2017
"if possible please use while or for ...loop to do" It is possible, but it is not a good way to write code. You should read this to know why, and what the better alternatives are (hint: indexing):

Connectez-vous pour commenter.

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 30 Août 2017
Modifié(e) : Andrei Bobrov le 30 Août 2017

0 votes

B = {B1;B2;B3;B4...;Bi}; % Please create B array.
n = cellfun(@numel,B);
A = [cell2mat(B),repelem((1:numel(n))',n)];

5 commentaires

ha ha
ha ha le 31 Août 2017
In your code, you don't use "the initial matrix A=zeros(1000,2) " , right?
Andrei Bobrov
Andrei Bobrov le 31 Août 2017
The stupid question.
Where is the source data?
ha ha
ha ha le 31 Août 2017
Modifié(e) : ha ha le 1 Sep 2017
Sorry. Because I run your your code with my example. I need to use the matrices B1,B2...Bi... data only. I don't use the input matrix A=zeros(1000,2). It also show the result. So, that's why I ask you?
clear; clc;
B1=[4; 1; 3] ;
B2=[2; 5; 7; 6];
B = {B1;B2}; % Please create B array.
n = cellfun(@numel,B);
RESULT = [cell2mat(B),repelem((1:numel(n))',n)];
or
clear; clc;
B1=[4 1 3] ;
B2=[2 5 7 6];
B = {B1';B2'}; % Please create B array.
n = cellfun(@numel,B);
RESULT = [cell2mat(B),repelem((1:numel(n))',n)];
I try to run this code, and the the result is the same as my expectation---> GOOD CODE
But actually, I wanna assign matrix B1,B2,B3...Bi to the first colum of matrix A (beacause i can not create the array B={B1;B2;B3;..;Bi} in my algorithm.
Guillaume
Guillaume le 31 Août 2017
You should never have matrix B1, B2, ... Bn in your workspace. As soon as you start numbering matrices, you should stop and change the way you work. These numbered matrices are obviously relatedm they should therefore be stored together into a single container variable such as the cell array that Andrei used.
ha ha
ha ha le 31 Août 2017
Modifié(e) : Stephen23 le 1 Sep 2017

Connectez-vous pour commenter.

Plus de réponses (2)

KL
KL le 30 Août 2017

0 votes

N = [B.' C.';
D.' E.'];
F = A+N
Why E has only 3 elements? I'd assume you missed out something there.

1 commentaire

ha ha
ha ha le 30 Août 2017
Modifié(e) : ha ha le 30 Août 2017
Yes. E should have 4 element (sorry I omitted it). please see my edit question

Connectez-vous pour commenter.

Guillaume
Guillaume le 30 Août 2017

0 votes

In your particular example, and assuming that E is supposed to have 4 elements instead of the 3 you've written:
F = A + [B.', C.'; D.', E.']
I have no idea where you're going with this question, so can't answer in a generic way.

1 commentaire

ha ha
ha ha le 30 Août 2017
please see my edit question

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!