Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
matrix with different dimension or how to use cell
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!