Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Could anyone help me to solve the issue in the following code.

1 回表示 (過去 30 日間)
jaah navi
jaah navi 2019 年 7 月 29 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
U =[0 0.0987 0 0 0.0767;
0 0.0141 0 0 0.0184]
V =[0 0.2948 0 0 0.2310;
0 0.4909 0 0 0.4257]
for v =1:size(V,2)
for u =1:size(U,1)
A(u,v) =((U(u-1,v-1)).*(V(u,v)))-sum((V(1:u-1,v)).*(U(u-1,v-1)))
end
end
In this code the expression A(u,v) does not need to perform for first row but it needs to be performed for second row.
Coud anyone please help me to solve this.
  5 件のコメント
jaah navi
jaah navi 2019 年 7 月 29 日
I already tried with 2 but i get all the values to be zeros.
Adam
Adam 2019 年 7 月 29 日
Just initialise
A = V;
before the loop as this appears to be the end result you want.
You should make it clear in the question what end result you want though. There is no reason for us to assume that you want the first row to be equal to that of V when you just say " does not need to perform for first row"

回答 (2 件)

Dvir Haberman
Dvir Haberman 2019 年 7 月 29 日
As Adam pointed out in the comments section: it seems like you're accessing index 0 of a MATLAB array. Unlike C, Python and others array indices in MATLAB starts in 1.
You could precalculate the first element and start your for loop from 2.
To start you loops from 2 simply do
for v =2:size(V,2)
for u =2:size(U,1)
A(u,v) =((U(u-1,v-1)).*(V(u,v)))-sum((V(1:u-1,v)).*(U(u-1,v-1)))
end
end
remember that you need to precalculate the first elements
  1 件のコメント
jaah navi
jaah navi 2019 年 7 月 29 日
i tried but unable to get the result.
What I actually need is the ouput should be as follows.
[ 0 0.2948 0 0 0.2310;
0 (0.4909-0.2948)*0.0987 0 0 (0.4257-0.2310)*0.0767].
[0 0.2948 0 0 0.2310;
0 0.0775 0 0 0.0149]
Could you please help me on this.

Guillaume
Guillaume 2019 年 7 月 29 日
What I actually need is the ouput should be as follows:
[ 0 0.2948 0 0 0.2310;
0 (0.4909-0.2948)*0.0987 0 0 (0.4257-0.2310)*0.0767]
[0 0.2948 0 0 0.2310;
0 0.0775 0 0 0.0149]
Hum,
>> (0.4909-0.2948)*0.0987
ans =
0.019355
not 0.0775.
Assuming the v-1 were a mistake and the offset was only on the rows, this is equivalent to the expression you wrote in your question and doesn't require loops:
A = [V(1, :); U(1:end-1, :) .* (V(2:end, :) - cumsum(V(1:end-1, :), 1))]

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by