take sum log of selected elements of matrix

2 visualizzazioni (ultimi 30 giorni)
Jay Hanuman
Jay Hanuman il 15 Nov 2016
Modificato: Jay Hanuman il 15 Nov 2016
I have 1000 length sequence x=[1 3 5 5 4 1 2 2 1 4 5 3 3 1 2 5 4......] and p matrix below.
1 2 3 4 5
1 0.0865 0.5096 0.1538 0.1346 0.1154
2 0.1070 0.4156 0.1317 0.2263 0.1193
3 0.0789 0.2105 0.0965 0.3070 0.3070
4 0.1194 0.0806 0.1290 0.3387 0.3323
5 0.0965 0.1754 0.0658 0.4474 0.2149
for window size 6 I am selecting 6 data points i.e. 1 3 5 5 4 1 so I want to do
log(P(1,3))+log(p(3,5))+log(p(5,5))+log(p(5,4))+log(p(4,1)).
now I am sliding window by 1 to next i.e. adding next element and skipping previous element of sequence i.e. 3 5 5 4 1 2 & want to do same thing i.e.
log(P(3,5)+log(p(5,5))+log(p(5,4))+log(p(4,1))+log(p(1,2))
and this is upto last of sequence i.e. for 1000 length sequence this operation has 995 times. how to do this.
  1 Commento
John D'Errico
John D'Errico il 15 Nov 2016
Is the window size of varying length? Or is it fixed at 6 elements?
You state that for a vector if indices of length 1000, there will be 1995 such operations. Sorry, but as you have explained it, it looks like only 995 operations, NOT 1995.
So you need to be more clear when you ask a question. Otherwise, we are forced to guess.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 15 Nov 2016
I think since the elements of x could have virtually any index values, you'll have to do this in a for loop. And I assume you know that P and p are different variables since MATLAB is case sensitive. So, something like
x = [1 3 5 5 4 1 2 2 1 4 5 3 3 1 2 5 4......]
windowLength = 6;
for windowStart = 1 : length(x)-windowLength+1
out(windowStart) = log(P(1,3)); % Initialize with the Capital P array element.
for k = 1 : windowLength - 1
row = x(k);
col = x(k+1);
out(windowStart) = out(windowStart) + log(p(row, col));
end
end
  1 Commento
Torsten
Torsten il 15 Nov 2016
I think the OP meant something like
x = [1 3 5 5 4 1 2 2 1 4 5 3 3 1 2 5 4......]
windowLength = 6;
for windowStart = 1 : length(x) - windowLength + 1
out(windowStart) = 0.0;
for k = windowStart : windowStart + windowLength - 2
row = x(k);
col = x(k+1);
out(windowStart) = out(windowStart) + log(p(row, col));
end
end
Best wishes
Torsten.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by