Subscripted assignment dimension mismatch. error in for loop

for i = 1 : (length(locs_min)*2) + 1;
output2(:,i+1) = output(locs_min(i):locs_max(i));
i = i + 1;
n = n + 1;
end
gives error:
|Subscripted assignment dimension mismatch.
Error in Ralphuh (line 74) output2(:,i+1) = output(locs_min(i):locs_max(i));|
output2 is a new variable, output is a 53957x1 double array, and locs_min and locs_max are both 1x538 double arrays.
I have tried transposing the different matrices, changing around the format of my indices, etc...nothing seems to work, please help, thanks!

回答 (3 件)

dpb
dpb 2015 年 11 月 2 日

0 投票

Unless the difference between the indices is the same for each iteration, you'll "go boom" the first time the length is different than the preceding.
Use a cell array in this case instead...Matlab's only support for "jagged" arrays.

2 件のコメント

Ralph
Ralph 2015 年 11 月 2 日
Hi dpb,
I am trying to avoid a cell array, but thank you. Can you please clarify what you mean by the following:
"Unless the difference between the indices is the same for each iteration, you'll "go boom" the first time the length is different than the preceding."
dpb
dpb 2015 年 11 月 3 日
Precisely what you see as the error and go on to explain is similar to what I presumed; the lengths of the various subsections aren't the same from one step to the next. If, say the first is min-max -->[1 20] the array will have been allocated by the first assignment as 1,20. Now if the next two locations are [21 40], you're fine but as soon as one isn't precisely 20 elements, then the assignment to that row will be off in the second dimension.
for i=1:length(locs_min)
output2{i} = output(locs_min(i):locs_max(i));
end
output will now be a cell array of the number of elements in the two location arrays. The {} curlies in the indexing instead of () normal parens make it such.

サインインしてコメントする。

Star Strider
Star Strider 2015 年 11 月 2 日

0 投票

What do you want to do?
You might be able to get there more easily with the reshape function.

5 件のコメント

Ralph
Ralph 2015 年 11 月 2 日
So, I have an oscillating signal, one cycle goes from negative to positive, the next cycle goes from positive to negative, which repeats.
locs_min and locs_max are the locations of the minima and maxima, which I am trying to use to define two double arrays with empty columns every other column.
Ralph
Ralph 2015 年 11 月 2 日
This is what I have now, but it gives me a cell array...which I can't work with because I have doubles of different lengths
i = 1;
n = 1;
%defines output2 as the noninverted portions of the signal.
%HOW DO I MAKE THIS INPUT INTO A NUMERICAL ARRAY?!
for i = 1 : length(locs_min);
output2(:,i) = output(locs_min(:,i):locs_max(:,i));
i = i + 1;
n = n + 1;
end
i = 1;
n = 1;
%defines output3 as the inverted portions of the signal.
%HOW DO I MAKE THIS INPUT INTO A NUMERICAL ARRAY?!
while i < length(locs_min)
output3{i} = output(locs_max(i):locs_min(i+1));
i = i + 1;
n = n + 1;
end
Star Strider
Star Strider 2015 年 11 月 3 日
If your signal is aperiodic (the lengths of the positive deflections are each different and the lengths of the negative deflections are each different), a cell array is the only way to go, unless you want to pad a rectangular matrix with strings of NaN values. Cell arrays require a bit more programming effort but are efficient and preferable in some instances.
Ralph
Ralph 2015 年 11 月 3 日
Ok...so I have a cell array of non-uniform doubles...I want to reshape them to be about 10 columns and 100 rows...then plot the average of the data as if I was plotting mean(numeric array, 10).
then plot the diff as if I was plotting diff(numeric array, 10)...how would I do that? I can't figure it out for the life of me.
Star Strider
Star Strider 2015 年 11 月 3 日
What you want to do will only work if the vectors are at least (or can be truncated to be exactly) 1000 elements long.
Guessing here since I don’t know how your data are organised, but something like this should work:
Vc = {1:1111}; % Original Cell Vector
V = Vc{1}(1:1000); % Original Cell Vector (Truncated)
V = V(:); % Convert To Column Vector
Vr = reshape(V(:), 100, 10); % Reshape To Desired Matrix
drVr = diff(Vr, [], 2); % Differences Along Columns
dcVr = diff(Vr, [], 1); % Differences Along Rows
I’m not quite certain what you want with respect to diff, so I present you with two options.

サインインしてコメントする。

Ralph
Ralph 2015 年 11 月 3 日

0 投票

currently working on inputting my data into zero matrices...thanks for your help

1 件のコメント

Star Strider
Star Strider 2015 年 11 月 3 日
My pleasure.
Note that the zeros are valid data. It might be best to pad them with NaN instead. However, I would see if the code in my comment to my Question does what you want before your start with padded matrices.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

質問済み:

2015 年 11 月 2 日

コメント済み:

2015 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by