Info

This question is closed. Reopen it to edit or answer.

matrix with different dimension or how to use cell

1 view (last 30 days)
GMDI
GMDI on 2 Sep 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, I have a problem:
S=[6000 6000 6000];
D=[1120 1680 1760];
P= [3 0 1
2 0 0
0 0 0];
I want to create a R matrix (here it is same as P matrix) like below from P based on the S and D,
R= [3 0 1
2 0 0
0 0 0];
which means R(i,j) = P(i,j) if if D(1,P(i,j)) <= S(1,j)when sum(P(:,j))> 0 ; here i and j are row and column of P, respectively. I wrote the following code to get R from P based on the above condition and works fine:
%%%%%%%%%%%% Matlab Code
S=[6000 6000 6000];
D=[1120 1680 1760];
P= [3 0 1
2 0 0
0 0 0];
R = zeros (size(P,1), size(P,2));
for i=1:size(R,1)
for j=1:size(R,2)
if sum(P(:,j))> 0
if P(i,j) >0
if D(1,P(i,j)) <= S(1,j)
S(1,j) = S(1,j) - D(1,P(i,j));
R(i,j) = P(i,j);
else
R(i,j)=0;
end
else
R(i,j) = 0;
end
else
R(:,j) = 0;
end
end
end
%%%%%%%%%%%%
Let’s say now my new D is :
D=[1120 5800 5400];
Of course D(1,P(1,1)) = 5400 satisfied by S(1,1), but D(1,P(1,2)) = 5800 cannot be satisfied by S[1,1] as We have only 600 left in S(1,1)
So what I want now is to create a following R matrix or something like this where I need to fulfill all D(1,nonzeros of P) using S. Even though We have S[1,1] =6000 , but we can have multiple S[1,1]=6000 as long as we satisfy all D(1, P(i,j)) , but S[1,1] cannot be greater than 6000 once at a time. Same thing for S[1,2] and S[1,3]
Now my new R will be (now the dimesion of R is 3*4, but previously it was 3*3) :
R=[3 0 0 1
0 2 0 0
0 0 0 0];
Here the first two columns of R will be under first column of P which means
Column 1 of R will be : 1 -> 3 -> 0 -> 0 [Here “1” is column number of P]
Column 2 of R will be : 1 -> 0 -> 2 -> 0 [Here “1” is column number of P]
Column 3 of R will be : 2 -> 0 -> 0 -> 0 [Here “2” is column number of P]
Column 4 of R will be : 3 -> 1 -> 0 -> 0 [Here “3” is column number of P]
I am not sure if it is possible to use “cell” to get above R matrix where first two column is under first column of P (based on new D). Can anyone please help me? I never used “cell” before and not familiar with that.
Thanks in advance. Please let me know if my question is not clear.

Answers (1)

Shadaab Siddiqie
Shadaab Siddiqie on 20 Nov 2020
To know more about cells refer here. Also refere this link for more information.

Community Treasure Hunt

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

Start Hunting!