Efficiently creating a partially filled NaN array with non-identical numbers of vals per row?

Hi everyone, quick question. I'm trying to optimize my code to handle data sets larger than one item for a specific equation. I wanted to do this in order to perform mxn matrix functions instead of sequential 1xn vector algebra. The primary stoppage for this is the following mathematical function. Vars defined as:
M - a small scalar integer variable used elsewhere in my code
k - index of a parent for loop that iterates 1:M
m - number of samples
n - [m M] array of integers
j - [m M] array of values 0-1
So the complication is in the following code line 6, which works fine for a single sample but breaks when size(n,1)>1
inter1 = 0
for k=1:M
inter1 = inter1 + ( (n(:,k) .*0.25.*pi .* rf(:,k).^4) + ...
pi .*rf(:,k).^2 .*rc(:,k).^2 .* ...
sum(sin(2*pi .*( ...
[1:n(:,k)] ...
+j(:,k)-1) ./n(:,k)).^2),2 );
end
Ideally I was hoping to optimize the section `[1:n(:,k)]` to yield the following result
>>n=[2 3 5; 3 2 4; 1 2 4];
>>f(n(:,2))
= [ 1 2 3
1 2 NaN
1 2 NaN ]
>>f(n(:,1))
= [ 1 2 NaN
1 2 3
1 NaN NaN ]
I'm specifically looking for NaN instead of 0 because if there were not that many objects (as indicated by n(i,k)) then they will not exist in a row summation. My best attempt at this is
newArray = nan(m,max(n(:,k)))
for i=1:m
newArray(i,1:n(i,k)) = 1:n(i,k)
end
And then proceeding with the code as before. Ultimately, the goal is:
>>size(A)
[m 1]

4 Comments

Where is the logic that specifies where in your array the NaNs are supposed to go?
First line of the fourth code quote? I don't have it generating a NaN array yet, as I'm still trying to find my way to the test case (code quote 3 and 5).
The real-world response to that question is "wherever the row max(1:n(i,k)) < max(n(:,k))" or maybe better stated as
newArray(i,1:n(i,k)) = [1:n(i,k) nan(1,max(n(:,k))-n(i,k))];
I don't believe so, no (but thanks for showing me that, I didn't know about it and I think I can leverage that). I'm just trying to step through `k` columns of `n` and make a row in a new variable that counts from 1 to some double digit number, with the following row doing the same for another sample.

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 1 Jul 2015

Edited:

on 1 Jul 2015

Community Treasure Hunt

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

Start Hunting!