Adding values to cells as the indices increase

조회 수: 6 (최근 30일)
Holmbrero
Holmbrero 2020년 12월 22일
댓글: Holmbrero 2021년 1월 4일
Hi!
I have a n x m cell array and i want to add a multiple of a constant as n and m increases.
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3) ---- add constant to row nr 2.
(3,1) (3,2) (3,3) ---- add constant x2 to row nr 3.
I would also like to add a constant to columns as they increase.
Any suggestions?
  댓글 수: 3
Holmbrero
Holmbrero 2020년 12월 22일
I'll try to elaborate.
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
I want to add a constant to row nr 2 ((2,1) (2,2) (2,3) ) and a multiple of the same constant to row nr 3 ((3,1) (3,2) (3,3) )
I also want to add another constant to column nr 1 ((1,2) (2,2) (2,3)) and a multiple of the same constant to columnt 3 ((1,3) (2,3) (3,3)).
I could just add them manually but i need the solution to work on a arbitrary nxm cell array.
Did that make my question easier to interpret?
Regards
Aaron Fredrick
Aaron Fredrick 2020년 12월 23일
Yes I understand your question but, just to blear could you tell as the matrix increases each time,
a constant is added to 2nd row and a multiple of that to the 3rd row, and another constant to 1st column and a multiple of that to the 3rd column
OR
a constant is added to 2nd row and a multiple of that to the last row, and another constant to 1st column and a multiple of that to the last column
And, is the the multiplication of the constant is same for each time?

댓글을 달려면 로그인하십시오.

채택된 답변

Gaurav Garg
Gaurav Garg 2020년 12월 28일
Hi Holmbrero,
I understand that you wish to add multiples of constants to each row. I am providing with a pseudo-code to help you do that -
for i=1:size(c,1)
for j=1:size(c,2)
c{i,j} = c{i,j} + constant * i
end
end
Assuming that 'c' is your cell array, the above code would add each cell element with a constant, depending on the row number.
You can similarly replicate the code to add each element with constant, depending on column number, like this -
for i=1:size(c,1)
for j=1:size(c,2)
c{i,j} = c{i,j} + constant * j
end
end
  댓글 수: 1
Holmbrero
Holmbrero 2021년 1월 4일
Hi!
Sorry for my late reply.
I solved it by creating two new matrices with the added constants and then simply added these to the cell array.
The problem was somehow sort of hard to describe but i think your solution is also appliciable.
Thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by