Info
此问题已关闭。 请重新打开它进行编辑或回答。
matrix with different dimension or how to use cell
2 次查看(过去 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!