I have a matrix of order 3*7
12 10 8 22 18 20 30
10 5 12 18 15 22 28
13 30 32 12 11 16 25
I want to add each element of every to each element of other rows
i.e. a1 a2 a3 a4 a5 a6 a7
c1 c2 c3 c4 c5 c6 c7 b1 b1 . . . . .
c1 c2 c3 c4 c5 c6 c7 b2 b2 . . . . .
c1 c2 c3 c4 c5 c6 c7 b3 b3 . . . . .
c1 c2 c3 c4 c5 c6 c7 b4 b4 . . . . .
c1 c2 c3 c4 c5 c6 c7 b5 b5 . . . . .
c1 c2 c3 c4 c5 c6 c7 b6 b6 . . . . .
c1 c2 c3 c4 c5 c6 c7 b7 b7 . . . . .
How can I do this using for loop ?

2 Comments

Hard to understand. Show the output you want using a smaller input matrix.
Mohammad Juned
Mohammad Juned on 12 Mar 2021
Edited: Mohammad Juned on 12 Mar 2021
let a a matrix of 3*3
1 2 3
4 5 6
7 8 9
now we add each element such as
1 + { (4+7) (4+8) (4+9)
(5+7) (5+8) (5+9)
(6+7) (6+8) (6+9) }
2+{ (4+7) (4+8) (4+9)
(5+7) (5+8) (5+9)
(6+7) (6+8) (6+9) }
3+{ (4+7) (4+8) (4+9)
(5+7) (5+8) (5+9)
(6+7) (6+8) (6+9) }
since this is a matrix of 3*3 order size therefore we have 3*3*3 i.e.27 elements in it.
for 3*7 we would have 7*7*7 i.e. 343 elements.

Sign in to comment.

 Accepted Answer

m=[12 10 8 22 18 20 30
10 5 12 18 15 22 28
13 30 32 12 11 16 25];
b=m(3,:)+m(2,:)';
for k=1:size(m,2)
M(:,:,k)=m(1,k)+b;
end

9 Comments

still not getting the right answer b is just adding up the b1 to c1 b2 to c2 b3 to c3 and so on
23 35 44 30 26 38 53
I want to add b1 to all elemnts of row c i.e.
10+13, 10+30, 10 +32 ........10+25
5+13, 5+30, 5+32......................5+25
.
.
.28+13, 28+30, 28+32............28+25
and then adding all these outcome to all elements of a.
Works for your examples. If I am not understanding, you will need to explain further.
M(:,:,1) =
35 52 54 34 33 38 47
30 47 49 29 28 33 42
37 54 56 36 35 40 49
43 60 62 42 41 46 55
40 57 59 39 38 43 52
47 64 66 46 45 50 59
53 70 72 52 51 56 65
M(:,:,2) =
33 50 52 32 31 36 45
28 45 47 27 26 31 40
35 52 54 34 33 38 47
41 58 60 40 39 44 53
38 55 57 37 36 41 50
45 62 64 44 43 48 57
51 68 70 50 49 54 63
M(:,:,3) =
31 48 50 30 29 34 43
26 43 45 25 24 29 38
33 50 52 32 31 36 45
39 56 58 38 37 42 51
36 53 55 35 34 39 48
43 60 62 42 41 46 55
49 66 68 48 47 52 61
M(:,:,4) =
45 62 64 44 43 48 57
40 57 59 39 38 43 52
47 64 66 46 45 50 59
53 70 72 52 51 56 65
50 67 69 49 48 53 62
57 74 76 56 55 60 69
63 80 82 62 61 66 75
M(:,:,5) =
41 58 60 40 39 44 53
36 53 55 35 34 39 48
43 60 62 42 41 46 55
49 66 68 48 47 52 61
46 63 65 45 44 49 58
53 70 72 52 51 56 65
59 76 78 58 57 62 71
M(:,:,6) =
43 60 62 42 41 46 55
38 55 57 37 36 41 50
45 62 64 44 43 48 57
51 68 70 50 49 54 63
48 65 67 47 46 51 60
55 72 74 54 53 58 67
61 78 80 60 59 64 73
M(:,:,7) =
53 70 72 52 51 56 65
48 65 67 47 46 51 60
55 72 74 54 53 58 67
61 78 80 60 59 64 73
58 75 77 57 56 61 70
65 82 84 64 63 68 77
71 88 90 70 69 74 83
When I am running your code the out shows like this but your output is showing differently
allcomb
23 35 44 30 26 38 53
(:,:,1) =
35 47 56 42 38 50 65
(:,:,2) =
33 45 54 40 36 48 63
(:,:,3) =
31 43 52 38 34 46 61
(:,:,4) =
45 57 66 52 48 60 75
(:,:,5) =
41 53 62 48 44 56 71
David Hill
David Hill on 12 Mar 2021
Edited: David Hill on 12 Mar 2021
If you copy and paste my code above in your command window and run, you will get my output.
I have copied and pasted this code but still not getting the answer
m=[12 10 8 22 18 20 30
10 5 12 18 15 22 28
13 30 32 12 11 16 25];
b=m(3,:)+m(2,:)';
for k=1:size(m,2)
M(:,:,k)=m(1,k)+b;
end
What release of matlab are you using? You may have to change this line
b=m(3,:)+m(2,:)';%if this is not giving you the correct matrix
with:
b=repmat(m(3,:),size(m,2),1)+repmat(m(2,:)',1,size(m,2));
Thank you David Hill I have got the right output. One more query
how can i save this 343 values in a single matrix
It already is saved in a single matrix M.
I am using R2014a
I have to to do more operation on the matrix M. So can it be performed .

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2014a

Community Treasure Hunt

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

Start Hunting!