Exceeds the number of array elements (0).

Hi everyone,
I have got the error in the code below, What should I do to fix it?
Code
% convert finite values to indices between 1 and map length
n = size(map, 1);
inds = (img(valid)-bounds(1))/(bounds(end)-bounds(1))*(n-1);
inds = floor(min(max(inds, 0), n-1))+1;
Error
Index exceeds the number of array elements (0).
Error in double2rgb (line 44)
ins = (img(valid)-bounds(1))/(bounds(end)-bounds(1))*(n-1);

 Accepted Answer

Voss
Voss on 24 Jan 2023
Either img or bounds (or both) is empty.
Check img and bounds and figure out why one or both is not what you expect.

9 Comments

"img" and "bounds" are in the function "double2rgb.m" that I used in my code.
COPYRIGHT code removed. Please do not post copyright code on this forum. Original source:
I got the error in using the above function in my code below:
Code:
%Rescaling network features for plotting networks
grafo = graph(W); % converting matrix to graph list of edges
links = table2array(grafo.Edges); % converting the graph object into a table
wij = links(:,3); % taking only link weigths
a = 1; b = 10; % range for rescaling limits
wij = ( (wij - min(wij)).*(b-a) )/(max(wij) - min(wij)); % rescaling formula
wij = wij+0.5;
rgb = squeeze(double2rgb(wij, colormap(jet))); close % mapping values to RGB colors with pallete JET
a = 1; b = 10; % range for rescaling limits
wij = ( (wij - min(wij)).*(b-a) )/(max(wij) - min(wij)); % rescaling formula
wij = wij+0.5;
So, I could not deal with the error. Please help me.
Thanks in advance.
Like I said, if img is empty, then you'll get the error you got.
Maybe wij is empty because links has zero rows. I don't know because I don't have your data. You'll have to check on that.
Yes, you are right. wij is NaN. What should I do?
I will display codes before wij step by step below:
grafo = graph(W);
links = table2array(grafo.Edges);
wij = links(:,3);
a = 1; b = 10; % range for rescaling limits
wij = ( (wij - min(wij)).*(b-a) )/(max(wij) - min(wij)); % rescaling formula
wij = wij+0.5;
So, due to this NaN, rgb does not run:
rgb = squeeze(double2rgb(wij, colormap(jet)));
What should I do? Thanks in advance for your help.
Do not use double2rgb(). Use
rgb = ind2rgb(uint8(rescale(wij, 0, 255)), jet);
Yesss. It solved :) Thanksss a billion.
Also, I add 0.01 in the denominator of wij and it works:
wij = ( (wij - min(wij)).*(b-a) )/((max(wij) - min(wij))+0.01);
Due to wij is NaN.
I fixed the problem of wij, but I got the error in the next step which is:
Code:
%% step24: Plotting a Functional Network
figure
hold on;
% ind=find(isnan(wij));
% wij(ind)=0.01;
for ll = 1 : length(links) %% plotting links
line([EEG.chanlocs(links(ll,1)).X, EEG.chanlocs(links(ll,2)).X], [EEG.chanlocs(links(ll,1)).Y, EEG.chanlocs(links(ll,2)).Y], 'LineWidth', wij(ll), 'Color', rgb(ll,:) );
end
colormap(jet);
% colorbar
for nn = 1 : 4 %% plotting nodes
plot(EEG.chanlocs(nn).X, EEG.chanlocs(nn).Y, 'ro', 'MarkerSize', st(nn), 'MarkerFaceColor', 'y')
text(EEG.chanlocs(nn).X+0.5, EEG.chanlocs(nn).Y, EEG.chanlocs(nn).labels, 'FontSize', 11);
end
view([270 90]); %% rotating to vertical view
axis equal; axis off;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]) %EXPANDING FIGURE ON SCREEN
Error:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in NEW1 (line 452)
line([EEG.chanlocs(links(ll,1)).X, EEG.chanlocs(links(ll,2)).X], [EEG.chanlocs(links(ll,1)).Y, EEG.chanlocs(links(ll,2)).Y], 'LineWidth', wij(ll),
'Color', rgb(ll,:) );
We do not have your current code, and we do not have your data to test with.
As outside observers, we have no reason to expect that EEG.chanlocs is not (for example) a transfer function that does not have an X field at all. Or maybe EEG is a graph() object that you added a chanlocs property to. We have no way of knowing.
After the section above, for running code below I've got an error, what's the problem?
Code:
for ll = 1 : length(links) %% plotting links
line([EEG.chanlocs(links(ll,1)).X, EEG.chanlocs(links(ll,2)).X], [EEG.chanlocs(links(ll,1)).Y, EEG.chanlocs(links(ll,2)).Y], 'LineWidth', wij(ll), 'Color', rgb(ll,:) );
end
colormap(jet);
% colorbar
for nn = 1 : 4 %% plotting nodes
plot(EEG.chanlocs(nn).X, EEG.chanlocs(nn).Y, 'ro', 'MarkerSize', st(nn), 'MarkerFaceColor', 'y')
text(EEG.chanlocs(nn).X+0.5, EEG.chanlocs(nn).Y, EEG.chanlocs(nn).labels, 'FontSize', 11);
end
view([270 90]); %% rotating to vertical view
axis equal; axis off;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]) %EXPANDING FIGURE ON SCREEN
Error:
Error using line
Value should be a finite number greater than 0
Error in NEW1 (line 452)
line([EEG.chanlocs(links(ll,1)).X, EEG.chanlocs(links(ll,2)).X], [EEG.chanlocs(links(ll,1)).Y, EEG.chanlocs(links(ll,2)).Y], 'LineWidth', wij(ll),
'Color', rgb(ll,:) );
Thanks in advance for your help.
Bests,
Neda
My guess is that wij(ll) is NaN or Inf or a non-positive number.
You can see below that those cases throw the error you got:
try
line([1 2],[1 2],'LineWidth',NaN)
catch e
delete(gca())
disp(e.message);
end
Value should be a finite number greater than 0
try
line([1 2],[1 2],'LineWidth',Inf)
catch e
delete(gca())
disp(e.message);
end
Value should be a finite number greater than 0
try
line([1 2],[1 2],'LineWidth',0)
catch e
delete(gca())
disp(e.message);
end
Value should be a finite number greater than 0
try
line([1 2],[1 2],'LineWidth',-1)
catch e
delete(gca())
disp(e.message);
end
Value should be a finite number greater than 0

Sign in to comment.

More Answers (0)

Tags

Asked:

on 24 Jan 2023

Edited:

on 7 Feb 2023

Community Treasure Hunt

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

Start Hunting!