how to get adress position inside vector

hi!
please for some help
i have this vector
i need to some value of eld in a vector gravlo just in right position given in vector adress
but matlab say Subscript indices must either be real positive integers or logicals.
can someone help me how to fix it
gravlo=zeros(1,100);
eld =[ 0
-1.7222
0
7.0000
0
-1.7778
0
6.8889
0
-1.7778
0
7.0000
0
-1.7222
0
7.1111];
adress =[ 0
3
0
2
0
1
31
32
61
62
63
64
65
66
33
34 ];
gravlo(adress)=gravlo(adress)+eld.*20;

7 Comments

can you show us your expected result ?
i need to some value of eld in a vector gravlo just in right position given in vector adress
which value of 'eld' do you want in 'gravlo' vector?
thank for answering
but perhaps i dont explaine my self correctly
i have a big matrix over 1000 or more size (16,1000) name G witch containe adress of free element, the value zero is mean that the element dont need update or not free
and i have a other matrix gravlo who make sum for only the element need to be update according of adress G with the value of eld+somthing (something =20) for exemple
i use a loop
let's use loop for just 5 first element
like this
gravlo=zeros(1,100);
eld =[ 0
-1.7222
0
7.0000
0
-1.7778
0
6.8889
0
-1.7778
0
7.0000
0
-1.7222
0
7.1111];
G=[3 66 156 246 336
0 63 153 243 333
2 64 154 244 334
0 61 151 241 331
1 62 152 242 332
31 121 211 301 391
32 122 212 302 392
61 151 241 331 421
62 152 242 332 422
63 153 243 333 423
64 154 244 334 424
65 155 245 335 425
66 156 246 336 426
33 123 213 303 393
34 124 214 304 394];
for i=1:5
g=G(:,i)
gravlo(g)=gravlo(g)-eld*20;
end
%
%
i try this to axtract a non zero value this ... gravlo(g(g~=0))
but when a sum
the dimension is of course different
and i m not sure that the value is just in the corect adress according to g
for i=1:5
g=G(:,i)
gravlo(g(g~=0))
gravlo(g(g~=0))=gravlo(g(g~=0))-eld*20;
end
sorry for my bad explained
i m so confuse
At least, eld and G should have the same number of rows.
This is not true in your case.
Even if they had the same number of rows, if you remove indices from g as you do, the line
gravlo(g(g~=0))=gravlo(g(g~=0))-eld*20;
will no longer work.
Further, "gravlo" must be a column, not a row vector.
You have these array (gravlo,eld,address,G). still i am not clear about your expectation.can you show some result ? like 4/5 rows of your expected result
gravlo=zeros(1,100);
eld =[0;-1.7222;0;7.0000;0; -1.7778;0;6.8889;0; -1.7778;0;7.0000;0;-1.7222;0;7.1111];
adress =[0;3;0;2;0;1;31;32;61;62;63;64;65;66;33;34 ];
G=[3 66 156 246 336
0 63 153 243 333
2 64 154 244 334
0 61 151 241 331
1 62 152 242 332
31 121 211 301 391
32 122 212 302 392
61 151 241 331 421
62 152 242 332 422
63 153 243 333 423
64 154 244 334 424
65 155 245 335 425
66 156 246 336 426
33 123 213 303 393
34 124 214 304 394];
thank you Sir
i try this
i know its look like a sharabian!!!
but its work when i use my big matix with more than size=2120
so thank You for answering
clc
clear all
eld =[ 0
-1.7222
0
7.0000
0
-1.7778
0
6.8889
0
-1.7778
0
7.0000
0
-1.7222
0
7.1111];
G=[ 0 65 155 245 235
3 66 156 246 336
0 63 153 243 333
2 64 154 244 334
0 61 151 241 331
1 62 152 242 332
31 121 211 301 391
32 122 212 302 392
61 151 241 331 421
62 152 242 332 422
63 153 243 333 423
64 154 244 334 424
65 155 245 335 425
66 156 246 336 426
33 123 213 303 393
34 124 214 304 394];
gravlo=zeros(427,1);
for iel=1:5
g=G(:,iel)
ss=find(g)
g(g~=0)
[mm nn]=size(g(g~=0));
[ee qq]=size(eld);
if mm<ee
edd=eld(ss);
gravlo(g(g~=0))=gravlo(g(g~=0))-edd*20;%
else
gravlo(g)= gravlo(g)-eld*20;%
end
end
A simplified version of your code:
gravlo = zeros(427,1);
for iel=1:5
g = G(:,iel);
m = (g ~= 0);
gravlo(g(m)) = gravlo(g(m)) - eld(m) * 20;
end
Hints: Do not use "clear all" in productive code, because it is a waste of time only.
Use the buttons on top of the field for posting messages to format the code. This improves the readability.
oh !!!
thank you Sir,
its very nice and its work with a simply code
thanks very much Sir,

Sign in to comment.

 Accepted Answer

Copied from my comment as an answer:
gravlo = zeros(427,1);
for iel = 1:5
g = G(:, iel);
m = (g ~= 0);
gravlo(g(m)) = gravlo(g(m)) - eld(m) * 20;
end

More Answers (0)

Categories

Asked:

on 12 Mar 2022

Answered:

Jan
on 12 Mar 2022

Community Treasure Hunt

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

Start Hunting!