How to exclude zeros in a matrix?

9 ビュー (過去 30 日間)
Robert  Flores
Robert Flores 2019 年 4 月 3 日
編集済み: Jan 2019 年 4 月 4 日
Hello,
I am trying to get rid of the zero values in a matrix I have in V(:,:,i) in my for loop. However, I am getting an error, "Unable to perform assignment because the size of the left side is 297-by-448 and the size of the right side is 10611-by-1. Error in DIC_Data_Extraction (line 17) V(:,:,i) = nonzeros(data_dic_save.displacements(i).plot_v_ref_formatted);". Therfore, if you are able to help me out in resolving this error, it will be most appreciated, thanks. Below is a copy of my code. Also, unfortunatley, I can not attach my work space, so I hope this is enough information for you to help me out, thanks.
-Robert
% The displacements in the Y-direction
V = zeros(297,448);
% Vyy = zeros{20863,1};
for i = 1:8
V(:,:,i) = nonzeros(data_dic_save.displacements(i).plot_v_ref_formatted);
% Vyy{i} = {nonzeros(V(:,:,i))}
avg(i) = abs(mean(mean(V(:,:,i))));
med(i) = median(median(V(:,:,i)));
end
  4 件のコメント
Adam Danz
Adam Danz 2019 年 4 月 3 日
From your description, it seems like zeros aren't the problem. Your nonzeros() function is producing a columnar vector with 10611 elements and you're trying to store that in a 297x448 matrix.
Robert  Flores
Robert Flores 2019 年 4 月 3 日
Adam Danz, that is exactly my issue. I am sorry for my late response, but that is the issue I am dealing with.

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

採用された回答

Jan
Jan 2019 年 4 月 3 日
編集済み: Jan 2019 年 4 月 4 日
The cell is the right approach:
for i = 1:8
Vyy{i} = nonzeros(data_dic_save.displacements(i).plot_v_ref_formatted);
avg(i) = abs(mean(Vyy{i}));
med(i) = median(Vyy{i});
end
Other have explained already, that arrays must be rectangular.
Another option would be to set the zeros to NaN:
V = zeros(297, 448, 8); % Pre-allocate all 3 dimensions!
for i = 1:8
aV = data_dic_save.displacements(i).plot_v_ref_formatted;
aV(aV == 0) = NaN;
V(:, :, i) = aV;
avg(i) = abs(mean(aV, 'all', 'omitnan'));
med(i) = median(aV, 'all', 'omitnan'); % See comments
end
Attention: I guessed, that you want the median of all values. Then median(X, 'all') is equivalent to median(X(:)), which is not necessarily the same as median(median(X, 1), 2). With nonzeros the output is a vector and there is no difference. Please check this explicitly.
  1 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 3 日
+1, cell is the best and uncertainty friend of a programmer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by